Skip to content

Instantly share code, notes, and snippets.

@inakiarroyo
Last active March 15, 2018 13:18
Show Gist options
  • Save inakiarroyo/3e0b4abc985f913a1bb41af7f6dd28cd to your computer and use it in GitHub Desktop.
Save inakiarroyo/3e0b4abc985f913a1bb41af7f6dd28cd to your computer and use it in GitHub Desktop.
Redux React Tips
// 1. Using indices and selectors for storing and accessing state (Store data with an index. Access it with selectors.)
const state = {
"usersById": {
123: {
id: 123,
name: "Jane Doe",
email: "jdoe@example.com",
phone: "555-555-5555",
//...
},
//...
}
};
// If we need to lookup a specific user object, we simply access the state like so:
const user = state.usersById[userId];
// Rendering a simple list of users with these data structures.
const getUsers = ({ usersById }) => {
return Object.keys(usersById).map((id) => usersById[id]);
}
// Get just the selected users from our state like so:
const getSelectedUsers = ({ selectedUserIds, usersById }) => {
return selectedUserIds.map((id) => usersById[id]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment