C.roy
C.roy

Reputation: 11

react-redux: should I maintain store data redundant?

For example, I have an entity at the backend called jobs, frontend called different ajax to get data by different search filter such as "jobStatus = Canceled" etc... So should I maintain only one jobList in store? or maintain a couple of list match different ajax? Thank you for help.

Upvotes: 1

Views: 472

Answers (1)

Moe
Moe

Reputation: 3713

It's better to treat your redux store as if it is a database. Normalize your nested lists, store each entity only once and reference it by id in all other places. A good way to do this is to use the Normalizr library. https://github.com/paularmstrong/normalizr

from the redux official docs:

the recommended approach to managing relational or nested data in a Redux store is to treat a portion of your store as if it were a database, and keep that data in a normalized form

reasons:

When a piece of data is duplicated in several places, it becomes harder to make sure that it is updated appropriately.

Nested data means that the corresponding reducer logic has to be more nested or more complex. In particular, trying to update a deeply nested field can become very ugly very fast.

Since immutable data updates require all ancestors in the state tree to be copied and updated as well, and new object references will cause connected UI components to re-render, an update to a deeply nested data object could force totally unrelated UI components to re-render even if the data they're displaying hasn't actually changed.

Reference: http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html

Upvotes: 1

Related Questions