Typical Usage
Explore the typical usage of Redux-ORM to manage normalized relational data in Redux. Learn how to create efficient selectors, work with Session instances, and write generic reducers to update entity attributes while keeping components unaware of Redux-ORM internals.
We'll cover the following...
I primarily use Redux-ORM as a specialized “super-selector” and “super-immutable-update” tool. This means that I work with it in my selector functions, thunks, reducers, and mapState functions. Here’s some of the practices I’ve come up with.
Entity Selection
Because I consistently use the ORM singleton instance across my application, I’ve created a selector that encapsulates extracting the current entities slice and returning a Session instance initialized with that data:
import {createSelector} from "reselect";
import orm from "./schema";
export const selectEntities = state => state.entities;
export const getEntitiesSession = createSelector(
selectEntities,
entities => orm.session(entities),
);
Using that selector, I can retrieve a Session instance inside of a mapState function, and look up pieces of data as needed by that component.
This has a couple benefits. In particular, because many different mapState functions might be trying to do ...