Skip to content

Instantly share code, notes, and snippets.

@pokorson
Created August 22, 2017 12:13
Show Gist options
  • Save pokorson/dd9573f4d9c5cefbf007061c1d3c858a to your computer and use it in GitHub Desktop.
Save pokorson/dd9573f4d9c5cefbf007061c1d3c858a to your computer and use it in GitHub Desktop.
Normalized state shape example
const posts = [
{title: 'title 1', id: 1231},
{title: 'title 2', id: 1232},
{title: 'title 3', id: 1233},
{title: 'title 4', id: 1234},
{title: 'title 5', id: 1235},
{title: 'title 6', id: 1236},
{title: 'title 7', id: 1237},
{title: 'title 8', id: 1238},
{title: 'title 9', id: 1239},
];
const findPost = (postId) posts.find(p => postId === p.id);
const postsById = {
1231: {title: 'title 1', id: 1231},
1232: {title: 'title 2', id: 1232},
1233: {title: 'title 3', id: 1233},
1234: {title: 'title 4', id: 1234},
1235: {title: 'title 5', id: 1235}
};
postById["1234"] === postById.1234
const findPostById = (postId) => postsById[postId]
const updatePostById = (postId, post) => postsById[postId] = {...postsById[postId], ...post}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment