redux-store-templates
redux-store-templates
is a set of helpers that reduces boilerplate code needed to provide useful and commonly used patterns in designing store structure of redux based applicatons.
It's lightweight, dependency-free, can work with typescript and you can easily integrate it with your existing project as well as get rid of it.
Example of usage
list
If you have the following action defined:
type: '[PRODUCTS] fetching success' payload: uuid: 'p1' name: 'product 1' uuid: 'p2' name: 'product 2' // ...
Then you can use list
helper to create reducers this way:
// products/reducers.js;; // ... products:
What will produce the following structure in the store:
/* ..., products: { byId: { 'p1': { uuid: 'p1', name: 'product 1' }, 'p2': { uuid: 'p2', name: 'product 2' } ... }, ids: ['p1', 'p2', ...] }*/
Then you can create selectors standard way:
// products/selectors.js const selectProductsState = stateproducts; /** * @returns */const selectAllProducts = { const prouctsState = ; return productsStateids;} /** * @param * * @returns {ProductDefinition | undefined} */const selectProductById = { return byIdid;}
Or use provided helpers:
// products/selectors.js; const selectProductsState = stateproducts; /** * @returns */const selectAllProducts = ; /** * @param * * @returns {ProductDefinition | undefined} */const selectProductById = ;
You can handle more actions, like:
- adding,
- updating,
- removing
- clearing
as well as define your own - what may end up in configuration like this (real example):
// products/reducers.js;; // ... products:
which replaces 100+ lines reducer - this is where redux-store-templates
shines. ✨
More examples
Documentation
todo
Installation
npm install --save redux-store-templates
or
yarn add redux-store-templates
Caution
By default redux-store-templates
is written in ECMAScript 2018 language standard and it is recommended that you transpile it inside your project by your own (using ex. @babel/preset-env).
If you need older EcmaScript version, then import it from es2015
directory:
// import transpiled EcmaScript 2015 version;