T
T
titronfan2016-10-15 19:53:07
React
titronfan, 2016-10-15 19:53:07

How does mapStateToProps work in Redux?

Hello! I am new to Redux. I want to explain the picture on a test example.
There is a "search" reducer with its own state:

var searchState = {
  search: { value: '', type: 'ordernumber' }
};

There is a component Component_A , into which the value of "type" with state is "thrown":
Component_A = connect(function(state) {
  return {
    type: state.search.type,
  }
})(Component_A);

And there is a Component_B component inside which, with each keystroke, the value parameter is updated (dispatch(setValue(newValue)). It is clear that the entire state in the reducer is updated when the value parameter changes , BUT I don’t understand why mapStateToProps is arranged in such a way that in component Component_A does it constantly run the render method, taking into account that the type field does not change?,
i.e., if I pass the changing value field to the Component_B component - ok, do a second "render", but if I pass the type field to the Component_A component- which is essentially immutable - why is the render method fired?
Thanks to all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2016-10-19
@iNikNik

mapStateToProps has a check - "Have props changed?": if yes, the component is updated, if not, it is not updated.
In your case, it's possible that something else is causing component A to rerender... please provide more code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question