Soundararajan
Soundararajan

Reputation: 31

React JS mapStateToProps

Several of my colleagues are spreading the part of the state in mapStateToProps.

 const mapStateToProps = (state = {}) =>
  ({
    addUserForm: { ...state.addUser.addUserForm },
    addUserLabels: { ...state.addUser.addUserLabels }
  });

Is it OK to do it? It duplicates the part of the state. And when actual Redux state changes the component won't get re rendered. Right? Correct me if I am wrong please.

Upvotes: 1

Views: 28

Answers (1)

BugsArePeopleToo
BugsArePeopleToo

Reputation: 2996

Because the spread is using state that is passed into mapStateToProps the component will still re-render when the state changes. I am not sure what is gained by the use of this spread syntax. Looks to achieve exactly the same thing as:

const mapStateToProps = (state = {}) =>
  ({
    addUserForm: state.addUser.addUserForm,
    addUserLabels: state.addUser.addUserLabels
  });

Upvotes: 1

Related Questions