React-redux: State change doesn't fire rerender

Created on 4 Sep 2017  路  3Comments  路  Source: reduxjs/react-redux

I have the following behavior:

@connect((state) => {
  const props = {
    isLoading: getIsLoadingSelector(state),
    datafeched: getDataFeched(state),
  };


  // {isLoading: true, data: null} the first update. {isLoading: false, data: {...} } the second update
  console.log(props);


  return props;
})
class MyComponent extends PureComponent {
  componentWillReceveProps(newProps) {
    console.log(newProps); // It's only trigger for the first update  `{isLoading: true, data: null}`
  }
  render() {
    // ...
  }
}

Could it be a bug or am I missing something?

Most helpful comment

99% of the time, "my component isn't re-rendering" is due to accidental mutation of your state. See http://redux.js.org/docs/faq/ReactRedux.html#react-not-rerendering .

All 3 comments

99% of the time, "my component isn't re-rendering" is due to accidental mutation of your state. See http://redux.js.org/docs/faq/ReactRedux.html#react-not-rerendering .

@markerikson I don't think it's the case. mapStateToProps is called but the component is not re-rendered.

Yes, and if the individual fields in the object returned from mapState are the same references as they were before, then connect will think nothing has changed and not re-render. Please check your reducer logic.

Now, that said, looking at the example you've given, I would expect that datafeched changed from null to an object would trigger a re-render, so there may be something else going on. But, I can still safely say that this is highly unlikely to be a bug in connect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sajaddp picture sajaddp  路  3Comments

IbraheemAlSaady picture IbraheemAlSaady  路  3Comments

julienvincent picture julienvincent  路  4Comments

cpprookie picture cpprookie  路  3Comments

a6051529 picture a6051529  路  4Comments