React Pros对象与数组组件更新

时间:2020-09-18 13:33:56

标签: reactjs redux react-redux reselect

我有一个来自React的示例,其中组件更新取决于props是对象还是数组类型。使用reselect库和Redux store。它是否像这样,意味着新对象将不等于prevprops?如果可以这样工作,那么我如何使用redux中的对象和新道具,以便shouldComponentUpdate中发生对象更改时props对我有用?

https://sbqun.csb.app/

1 个答案:

答案 0 :(得分:1)

Reducer函数错误,CounterReducer.js,第10行

 return {
        ...state,
        // don't set this directly, this code preserves the reference to the array and react does not updates the 
        counter: state.counter
        // counter: Array.from(state.counter)
      };

检查我更新的代码,您正在上面的代码中设置引用,并用此代码替换上面的代码。

return {
        ...state,
        //counter is now a new array 
        counter: [...state.counter]
        // counter: Array.from(state.counter)
      };