Understanding React-Redux and mapStateToProps()
I'm trying to understand the connect method of react-redux, and the functions it takes as parameters. In particular
The way I understand it, the return value of
mapStateToProps will be an object derived from state (as it lives in the store), whose keys will be passed to your target component (the component connect is applied to) as props.
This means that the state as consumed by your target component can have a wildly different structure from the state as it is stored on your store.
Q: Is this OK?
Q: Is this expected?
Q: Is this an anti-pattern?
Is this ok?
Is this expected?
Yes, this is expected (if you are using react-redux).
Is this an anti-pattern?
A: No, this is not an anti-pattern.
It's called "connecting" your component or "making it smart". It's by design.
It allows you to decouple your component from your state an additional time which increases the modularity of your code. It also allows you to simplify your component state as a subset of your application state which, in fact, helps you comply with the Redux pattern.
Think about it this way: a store is supposed to contain the entire state of your application.
For large applications, this could contain dozens of properties nested many layers deep.
You don't want to haul all that around on each call (expensive).
mapStateToProps or some analog thereof, you would be tempted to carve up your state another way to improve performance/simplify.