I'm trying to understand the connect method of react-redux, and the functions it takes as parameters. In particular mapStateToProps()
.
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?
}
}
You got the first part right:
Yes
mapStateToProps
has the Store state as an argument/param (provided byreact-redux::connect
) and its used to link the component with certain part of the store state.By linking I mean the object returned by
mapStateToProps
will be provided at construction time as props and any subsequent change will be available throughcomponentWillReceiveProps
.If you know the Observer design pattern it's exactly that or small variation of it.
An example would help make things clearer:
There can be another react component called
itemsFilters
that handle the display and persisting the filter state into Redux Store state, the Demo component is "listening" or "subscribed" to Redux Store state filters so whenever filters store state changes (with the help offiltersComponent
) react-redux detect that there was a change and notify or "publish" all the listening/subscribed components by sending the changes to theircomponentWillReceiveProps
which in this example will trigger a refilter of the items and refresh the display due to the fact that react state has changed.Let me know if the example is confusing or not clear enough to provide a better explanation.
As for: 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.
I didn't get the question, but just know that the react state (
this.setState
) is totally different from the Redux Store state!The react state is used to handle the redraw and behavior of the react component. The react state is contained to the the component exclusively.
The Redux Store state is a combination of Redux reducers states, each is responsible of managing a small portion app logic. Those reducers attributes can be accessed with the help of
react-redux::connect@mapStateToProps
by any component! Which make the Redux store state accessible app wide while component state is exclusive to itself.This react & redux example is based off Mohamed Mellouki's example. But validates using prettify and linting rules. Note that we define our props and dispatch methods using PropTypes so that our compiler doesn't scream at us. This example also included some lines of code that had been missing in Mohamed's example. To use connect you will need to import it from react-redux. This example also binds the method filterItems this will prevent scope problems in the component. This source code has been auto formatted using JavaScript Prettify.
This example code is a good template for a starting place for your component.
Q:
Is this ok?
A: yes
Q:
Is this expected?
Yes, this is expected (if you are using react-redux).
Q:
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).
Without
mapStateToProps
or some analog thereof, you would be tempted to carve up your state another way to improve performance/simplify.Yes, it is correct. Its just a helper function to have a simpler way to access your state properties
Imagine you have a
posts
key in your Appstate.posts
And component
Posts
By default
connect()(Posts)
will make all state props available for the connected ComponentNow when you map the
state.posts
to your component it gets a bit nicermapDispatchToProps
normally you have to write
dispatch(anActionCreator())
with
bindActionCreators
you can do it also more easily likeNow you can use it in your Component
Update on actionCreators..
An example of an actionCreator:
deletePost
So,
bindActionCreators
will just take your actions, wrap them intodispatch
call. (I didn't read the source code of redux, but the implementation might look something like this:React-Redux
connect
is used to update store for every actions.It's very simply and clearly explained in this blog.
You can clone github project or copy paste the code from that blog to understand the Redux connect.