What's the point of normalizing api responses in a Redux app?

Maybe I would be fat and very new to Redux, but I would be interested if someone could give me a simple use case for something like normalizr .

I don’t understand the example here. How do I handle nested API responses in a Flux application?

I believe that I do not receive, because it is difficult for stores to consume embedded objects.

My application is a medium-sized portal where users can display lists, create new items and new lists while talking with two different APIs.

+5
source share
2 answers

I normalize all my objects, but I do not use normalizr.

I like to normalize because it makes the code more readable. An example is below. It also makes it easier to access objects and eliminate duplication. For example, if you subscribe to the todo element in the elses user list, you either need to return a duplicate version of this list in the subscribedTodos subscribedTodos , or you need to know the user ID and todo of the other todo in order to get to it.

Back to readability: Which ones are 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; } } 
+5
source

Consuming a nested object is not necessarily complicated.

It all depends on your use case and how your user interface is organized.

If you do not need to use normalizr, then do not use it. This module is really not a requirement.

+1
source

Source: https://habr.com/ru/post/1242106/


All Articles