0
votes

I am currently working on an React-Application where I use Immutable.js in my Redux-Reducers. The reducers will return the previous state as a reference if they have not changed it (as recommended). If I now compared the previous state with the next state (state===nextState) it would return true, since the reference has not changed, right?

Now if the reducer changed the state, it would return a new Immutable.js Map.

Doing the same comparision now it would return false, since it is a new Map.

Would it be reasonable to somehow detect prop changes within shouldComponentUpdate like that?

If so, is there a way to modify the react-redux connect function to use my custom shouldComponentUpdate method? Because apparently it is not possible to pass an Immutable.js Object as props to a component.

1
Welcome to SO. We prefer questions that can be answered, not just discussed. Please visit the help center and take this tour to see what to ask and how.B001ᛦ

1 Answers

1
votes

In mapStateToProps, if you are not converting this immutable iterable to equivalent JS object and pass Immutable object as it is. You can do achieve it.

For example:

Instead of

const mapStateToProps = (state) => ({ ...(state.toJS()) });

Way to access any data from state in the component: this.props.data

You can also write as:

const mapStateToProps = (state) => ({ state });

Way to access any data from state in the component: this.props.state.get('data')