Redux state shape without data duplication -


i'm new redux , it's hard grasp how implement state shape without duplicating data, in case need update it, , naive way update in few places, negate single source of truth.

we fetch user profile , posts api server:

www.api.com/users/placeholder { "user": { "username": "placeholder", "bio": "it's bio", "profileimage": "http://via.placeholder.com/350x150", "isviewerfollowing": false } } www.api.com/posts?author=placeholder { "posts":[{ "id": "1", "caption":"caption placeholder", "image":"http://via.placeholder.com/1000x1000", "createdat": "2017-08-18t03:22:56.637z", "updatedat": "2016-08-18t03:48:35.824z", "islikedbyviewer": false, "likescount": 0, "author": { "username": "placeholder", "bio": "it's bio", "profileimage": "http://via.placeholder.com/350x150", "isviewerfollowing": false, } }, { "id": "2", "caption":"caption placeholder", "image":"http://via.placeholder.com/1000x1000", "createdat": "2017-08-18t03:22:56.637z", "updatedat": "2016-08-18t03:48:35.824z", "isviewerliked": false, "likescount": 0, "author": { "username": "placeholder", "bio": "it's bio", "profileimage": "http://via.placeholder.com/350x150", "isviewerfollowing": false, } }], "postscount": 2 }

for example, have separate reducers users , posts, , user wants follow user/author, need update information in 2 reducers. final question be, hint me state shape in particular example ?

thanks!

you should normalize redux state: instead of saving entire author object every post, should save authorid.

since ensure when have post object in posts branch of redux state, have related author in authors branch, retrieve posts author's data can create selector:

export function getposts(reduxstate) {   return reduxstate.posts.map(post => {     const author = reduxstate.authors.find(a => a.id === post.authorid);     return {       ...post,       author     };   }); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -