Right way to update state in redux reducers

后端 未结 3 703
悲&欢浪女
悲&欢浪女 2020-12-24 14:20

I\'m a newbie in redux and es6 syntax. I make my app with official redux tutorial, and with this example.

There is JS snippet below. My point - to define REQUEST_PO

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 14:49

    If I understand correctly, you are having trouble getting the specific post you want.

    First of all, Having your reducer also update the array and the object in it, makes it hard to read and maintain. I suggest you watch this short video explaining about reducer composition with arrays. You can simplify your code by using the technique described there.

    In your case, you would a posts reducer and a post reducer, while posts reducer calls the post reducer.

    As for finding the right object to work on, Dan Prince's suggestion makes it easier. Having an object map instead of an array would make it easier for you. Relevant code snippet from Dan's answer:

    const initialState = {
      items: {
        3: {id:3, title: '1984', isFetching:false},
        6: {id:6, title: 'Mouse', isFetching:false}
      ];
    }
    

提交回复
热议问题