React/Redux rendering a list that's updating every second

后端 未结 5 947
生来不讨喜
生来不讨喜 2021-02-01 06:44

I have a react component that receives props from the redux store every second. The new state has an array that\'s different than the last array. To be specific, every second an

5条回答
  •  你的背包
    2021-02-01 07:13

    The problem really exists in the reducer.

    myList: [ payload, ...state.myList.filter(item => payload.id !== item.id).slice(0, -1) ]
    

    What is the logic implemented using slice(0,-1)?

    It is the culprit here.

    From your question I understood the next state after [1,2,3] will be [1,2,3,4].

    But your code will be giving [4,1,2], then [5,4,1] then [6,5,4].

    Now all the elements in the state are new, not in the initial state. See state is not just getting appended it is completely changing.

    Please see if you are getting the desired result by avoiding slice.

     myList: [ payload, ...state.myList.filter(item => payload.id !== item.id)]
    

提交回复
热议问题