August 2017
Beginner
374 pages
10h 41m
English
Once you have more complicated behavior in your mapStateToProps function, you might want to consider writing selector functions. They take the state as an argument and return a certain part of the state.
Consider we have an object of posts stored by their IDs:
{ "posts": { "0": { "id": 0, "author": "dan", "text": "hello world" }, ... }}
We could write a getPosts selector function, which returns a list of posts in an array:
const getPosts = ({ posts }) => { return Object.keys(posts).map( (id) => posts[id] )}
Consider we have an array of selected post IDs and only want to get those from the state:
{ "posts": { ... }, "selectedPosts": ["0", "2"]}
We could create another selector function to get all selected posts from the state: ...
Read now
Unlock full access