Redux-ORM

Redux-ORM

  • Getting Started
  • Docs
  • API
  • GitHub

›API Reference

Introduction

  • Getting Started
  • Installation

Basics

  • Quick Start
  • Relational Fields
  • Reducers
  • Selectors
  • Usage with React

Advanced

  • Complex Selectors
  • Custom Reducer

API Reference

  • API Reference
  • ORM
  • Model
  • QuerySet
  • Session
  • Fields
  • Redux

redux

Provides functions for integration with Redux.

level

Drill down into selector map by cachePath.

The selector itself is stored under a special SELECTOR_KEY
so that we can store selectors below it as well.

Kind: inner property of redux

defaultUpdater()⇒ undefined

Calls all models' reducers if they exist.

Kind: global function

createReducer(orm, [updater])⇒ function

Call the returned function to pass actions to Redux-ORM.

Kind: global function
Returns: function -

reducer that will update the ORM state.

ParamTypeDescription
ormORM

the ORM instance.

[updater]function

the function updating the ORM state based on the given action.

createSelector(...args)⇒ function

Returns a memoized selector based on passed arguments.
This is similar to reselect's createSelector,
except you can also pass a single function to be memoized.

If you pass multiple functions, the format will be the
same as in reselect. The last argument is the selector
function and the previous are input selectors.

When you use this method to create a selector, the returned selector
expects the whole redux-orm state branch as input. In the selector
function that you pass as the last argument, any of the arguments
you pass first will be considered selectors and mapped
to their outputs, like in reselect.

Here are some example selectors:

// orm is an instance of ORM
// reduxState is the state of a Redux store
const books = createSelector(orm.Book);
books(reduxState) // array of book refs

const bookAuthors = createSelector(orm.Book.authors); bookAuthors(reduxState) // two-dimensional array of author refs for each book

Selectors can easily be applied to related models:

const bookAuthorNames = createSelector(
    orm.Book.authors.map(orm.Author.name),
);
bookAuthorNames(reduxState, 8) // names of all authors of book with ID 8
bookAuthorNames(reduxState, [8, 9]) // 2D array of names of all authors of books with IDs 8 and 9

Also note that orm.Author.name did not need to be wrapped in another createSelector call,
although that would be possible.

For more complex calculations you can access
entire session objects by passing an ORM instance.

const freshBananasCost = createSelector(
    orm,
    session => {
       const banana = session.Product.get({
           name: "Banana",
       });
       // amount of fresh bananas in shopping cart
       const amount = session.ShoppingCart.filter({
           product_id: banana.id,
           is_fresh: true,
       }).count();
       return `USD ${amount * banana.price}`;
    }
);

redux-orm uses a special memoization function to avoid recomputations.

Everytime a selector runs, this function records which instances
of your Models were accessed.

On subsequent runs, the selector first checks if the previously
accessed instances or args have changed in any way:

  • If yes, the selector calls the function you passed to it.
  • If not, it just returns the previous result (unless you call it for the first time).

This way you can use pure rendering in your React components
for performance gains.

Kind: global function
Returns: function -

memoized selector

ParamTypeDescription
...argsfunction

zero or more input selectors
and the selector function.

Last updated on 10.1.2020
← Fields
  • level
Redux-ORM
Docs
Getting StartedQuick StartAdvanced scenarios
Community
Stack OverflowGitter
More
GitHubStar
Copyright © 2020 Redux-ORM documentation authors.
Some icons copyright Font Awesome.