Search code examples
reduxstorestate-management

Redux Organizing Normalized Data in State


Been playing around with adding state management to my application, and was hoping for a little clarification on best practices for organizing normalized data.

Based on the documentation in the Redux Docs a common pattern is to organize things like "authors" and "books" under "entities" inside your state.

However, most examples that I've come by doesn't fit this pattern. It seems as though a common practice is placing "authors" and "books" on their own inside the state rather than within "entities".

Just hoping to get some clarification on this, and an understanding of how to get "authors" and "books" within "entities" to fit the structure of the example docs.


Solution

  • I wrote that whole "Structuring Reducers" docs section, so I'm gonna say I'm qualified to answer :)

    Putting all the different normalized data under an entities key is just a suggestion. If you prefer to put each item type at the top of your state tree, that's fine and up to you.

    A number of my suggestions in the normalization docs pages are based off of how the Redux-ORM library organizes things. It generates a reducer function that defines all the "tables" for your different item types, so that reducer function usually becomes a slice in your root reducer (and is usually named either orm or entities).

    For more info on using Redux-ORM, you might want to read through my "Practical Redux" tutorial series, which shows a number of techniques for using Redux-ORM, and demonstrates its usage in a sample app.