A state selector to optimize the usage of React, Redux, Reselect & Normalizr.
Table of Contents
- How It Works
An avoidable re-render happens when a React component receives a shallow copy of one of it's properties, however the object didn't "really" change. Meaning A deep comparison of all of the object's primitives would find them identical. This causes a React component to run it's render method needlessly, since the render result would be identical to the last one.
yarn add renorm
npm install --save renorm
- Discovers entities used in your selector automatically.
- Significant performance boost when selecting a list of entities.
- Developer friendly syntax.
How It Works
When you create a renorm selector this is what happens:
- A new reselect selectorCreator
is created with the schema you provided.
Renorm uses a customized version of Reselect's
defaultMemoizeoptimized for normalizr entities.
- Renorm traverses the schema you provided and saves all unique entities found.
- Renorm creates an entities selector with only the relevant schema entities
- Renorm now uses the Reselect selector creator from #1 which uses input, schema and relevant entities to invoke Normalizr's
denormalizemethod. This is the return value when invoking
Renorm has a two phase memoization strategy
Basic Memoization. Based on Reselect's existing
- Check arguments length and null check
- Shallow equality of of the input ids and each one of the relevant entity maps (stocks, companies, etc.) if all the checks return true the memoized version is returned, otherwise the function is invoked. This the exact same process Reselect already does by default.
if the function is invoked Renorm switches to advanced memoization
For each the denormalized object, check all underlying entities:
- If all underlying entities are shallowly equal to the previous ones, return previous denormalized object.
- Else return the denormalized object
- Cache results for next run
The big advantage here is that only newly returned objects will trigger React's render. Without Renorm's advanced memoization every item on the list would trigger React's render, even if you only a single entity was changed!
// schema;const stockSchema = 'stocks';const companySchema = 'companies'stock: stockSchema;const Schemas =STOCK: stockSchemaSTOCK_ARRAY: stockSchemaCOMPANY: companySchemaCOMPANY_ARRAY: companySchema;// redux stateconst state =companyIds: 'COMP_A' 'COMP_B' 'COMP_C' /*...*/entities:companies:/*company entities...*/stocks:/*stock entities...*/;//renorm selector;const getCompanyIds = statecompanyIds;const getCompanies = ;
For comparison, here's the same selector without using renorm:
;;const getCompanyIds = statecompanyIds;const getCompanyEntities = stateentitiescompanies;const getStockEntities = stateentitiesstocks;const getCompanies =;
Renorm's third parameter is an optional options object that contains the following props:
|entitiesPath||string||"entities"||Path to the entities object in the state|
|process||function||(result) => result||A function to perform mutation operations before results are returned|