A
A
Andrey Prozorov2020-02-27 18:35:09
redux
Andrey Prozorov, 2020-02-27 18:35:09

How to properly design a redux store structure?

Good afternoon. Help with a hollivar question.

The question is not entirely about the store, but rather about the form in which to store data in the repository, but it was necessary to title it somehow.

There is an entity for example toDo. For example, it comes to us from the server like this:

{
   id: number;
   text: string;
   completed: boolean;
}


I'm making a component where I use a multiple selection of these items (ToDoList), in order to send a request to the server to complete them all at once in a batch:

POST server.com/complete
id[]

Where can I store the sign that the item is selected?

Two options:

1) in redux I will create a store with an array where I will add id[]

2) I will modify the model and add isSelected there for example
{
   id: number;
   text: string;
   completed: boolean;
   isSelected: boolean;
}


Or some other option ...

Please, just reasoned. Thanks

PS: Addition and complication to the task. There is a component in which, for example, you need to show the counter of selected toDo. This is in case you want to make the state local at the ToDoList level.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-02-27
@Xuxicheta

Of course, you don't need to modify the model.
The array with the id is also not very good.
The third option is to store the map, i.e. an object where keys are IDs and values ​​isSelected.
A more correct option is WeakMap, where the keys are the objects of the body, but I do not know how friendly it is with redux.
In general, a common practice is to create a parallel entity state with the same keys, which contains the current ui state of the models.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question