State as a Database
Explore how treating Redux state as a database of entities helps manage application data more effectively. Learn to normalize state by organizing data into tables with IDs, reducing nesting complexity. Understand how this structure simplifies reducer logic for actions like adding ingredients, improving maintainability.
We'll cover the following...
A recommended approach to solving the various issues raised in the previous lessons is to treat the application state as a database of entities. Like in a regular table-based database, we will have a “table” for each entity type with a “row” for each entity. The entity id will be our “primary key” for each table.
In our example, we will break down nesting to make our state as shallow as possible and express connections using IDs:
const state = {
books: {
21: {
id: 21,
name: 'Breakfast',
recipes: [63, 78, 221]
}
...