I normalize all my objects but I don't use normalizr.
I like to normalize because it makes code more readable. An example below. It also makes it much easier to reference objects and eliminate duplication. For example, if you subscribe to a todo item on someone elses list, you either have to return a duplicate version of that todo in the subscriber's subscribedTodos
list, or you have to know the user id and todo of the other todo in order to get to it.
Back to readability: Which of these is better to read/understand?
function rootReducer (state, action) {
const { type, payload } = action;
if(action.type === MODIFY_TODO) {
return {
...state,
users: {
...state.users,
[payload.userID]: {
...state.users[userID],
todos: {
...state.users[userID].todos,
[payload.todo.todoID]: {
...state[userID].todos[todoID],
...todo
}
}
}
}
}
} else { return state; }
}
function rootReducer (state, action) {
const { type, payload } = action;
if(type === MODIFY_TODO) {
return {
...state,
todos: {
state.todos[payload.todo.id]: {
...state.todos[payload.todo.id],
...payload.todo
}
}
}
} else { return state; }
}