- Notifications
You must be signed in to change notification settings - Fork4
🥒 Define and share reusable slices of Redux.
License
redux-definitions/redux-definitions
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
yarn add redux-definitions
TLDR - Define and share reusable slices of Redux.
Common reducer patterns always get recycled, write them once and then never repeat yourself again! Usedefinitions to automatically generate namespaced reducers, actions, and selectors for a predefined slice of functionality.
The example below implements the Redux code necessary for a basic shopping experience in 12 lines of code. This is achieved by leveraging an existing Collection definition and creating a custom Cart definition.
import{createReducers,createDefinition,Definitions}from'redux-definitions'const{ Collection}=DefinitionsconstCart=createDefinition({defaultState:[],reducers:{addItem:(state,{ payload})=>state.push(payload),clearItems:()=>[],},selectors:{getTotal:(state)=>state.reduce((total,item)=>total+item.price,0)}})const{ actions, reducers, selectors}=createReducers({shopping:{items:Collection,cart:Cart}})
BONUS
Use the browser console to call actions and selectors against the running Redux application.
Inspired by lessons learned building countless UIs, Redux Definitions is a library thatabstracts common Redux reducer patterns into reusable definitions that can be shared and used to generate standardized and logically related actions, reducers, and selectors.
Redux Definitions is 100% compatible with any existing Redux-based project.
To help organizations scale development, maintainability, and velocity on Redux-based projects.
- Library of reusable reducer definitions (Flag, Field, Collection, Index, etc)
- Automatically generated actions, reducers, and selectors.
- In-browser Redux-REPL for interacting with running application.
yarn add redux-definitions
As shown above your core application state is described using a library ofreducer definitions.createReducers takes these definitions and creates reducers for you with corresponding actions and selectors.
import{createReducers,Definitions}from'redux-definitions'const{ Collection, Flag, Field, Index}=Definitionsconst{ actions, reducers, selectors}=createReducers({todoList:{todos:Collection,completedIds:Index,selectedIds:Index},todoEditor:{isEditing:Flag,editingId:Field}})
Each top-level key in the
createReducersschema generates a separate reducer.
Action creator functions are returned fromcreateReducers calls asactions. The reducer definition determines what actions are available. For example aCollection has actionscreate,upsert,remove,set,reset,clear. Learn more about what actions are available and what their expected payloads look like thereducer definitions section.
const{ todoList, todoEditor}=actionstodoList.todos.create({id:'1',message:'Do thee laundry'})// { type: 'todos/create', payload: { id: '1', message: 'Do thee laundry' }}todosList.todos.update({id:'1',message:'Do the laundry'})// { type: 'todos/update', payload: { id: '1', message: 'Do the laundry' }}todoEditor.editingId.set('1')// { type: 'todoEditor/editingId/set', payload: '1' }todoList.todos.remove('1')// { type: 'todos/remove', payload: '1' }todoList.selectedIds.add('1')// { type: 'selected/add', payload: '1' }}todoList.selectedIds.clear()// { type: 'selected/clear' }
Selectors are also returned fromcreateReducers. For example aCollection hasall,find,ids, andcount:
const{ todoList, todoEditor}=selectorstodoList.todos.all(state)// returns a collection of todostodoList.todos.find(state,{ id})// returns a todo with matching `id`todoList.todos.ids(state)// returns an array of ids
These selectors are perfect for feeding intoReselect
When in dev-mode Redux Definitions automatically provides a REPL-like experience in the browser console for dispatching pre-bound actions and selectors. Actions and selectors from allcreateReducers calls are available in the REPL.
For your convenience unlike normal actions and selectors, calls to actions and selectors in the browser console are pre-bound to
store.dispatchandstore.getState. Remember, only in the console!
To setup the REPL, importstartRepl and call it on your project'sstore object.
import{startRepl}from'redux-definitions'...conststore=createStore(rootReducer,initialState,applyMiddleware(..))startRepl(store)
Note: when server-side rendering this call will be a no-op.
All reducer definitions acceptinitialState values.
import{createReducers,Definitions}from'redux-definitions'const{ Collection, Flag, Field}=Definitionsconst{ reducers}=createReducers({todoList:{todos:Collection({initialState:[{id:'1',message:'Do the laundry'}]}),completedIds:Index({initialState:['1']}),selectedIds:Index},todoEditor:{isEditing:Flag({initialState:true}),editingId:Field({initialState:'fooId'})}})
Redux Definitions provides an assortment of reducer definitions that can be found by importing theDefinitions object. Reducer definitions aim to be low-level enough to be generic but high level enough to abstract state patterns common to all applications.
Field creates a simple reducer that stores any value and comes with action types that set and clear.
set(payload: any)Sets Field to payload value.
clear(void)Clears current Field state.
get(state: {}): anyReturns the Field state.
isSet(state: {}): booleanReturns a boolean specifying whether a Field value is set.
Record creates a reducer that stores an object. The object can be set or updated (similar to setState).
set(payload: {})Sets Record state to payload object.
update(payload: {})Merges payload object into the current Record state.
clear(void)Clears Record and sets it to an empty object.
get(state: {}, keys?: string|string[]): {}Returns the Record object, optionally takes keys to return a subset of the Record.
keys(state: {}): string[]Returns an array of the Record's existing key names.
Flag creates a reducer that stores a boolean value and comes with actions for setting and toggling.
set(payload?: boolean)Sets Flag to true or to the optional payload's boolean value.
unset(void)Sets Flag to false.
toggle(void)Toggles Flag value.
get(state: {}): booleanReturns the current Flag value.
Collection creates a reducer that storesEntities.Entities are objects withid properties. Entities can take any form as long as they at least have an id.
Id = stringEntity = { id: Id }
set(payload: Entity[])Takes an array of entities. Resets entire Collection to the payload of entities.
reset(void)Resets the entire Collection to empty.
create(payload: Entity)Takes an Entity and adds it to the Collection. Warning will be logged if an entity with theid already exists.
update(payload: Entity)Takes an Entity and updates it in the Collection. Entity is not added unless an entity with theid already exist.
upsert(payload: Entity)Takes an Entity and updates it in the Collection. The entity will be added if an entity with theid does not exist.
remove(payload: Id|Id[])Takes an Id and removes any existing entity with theid.
all(state: {}): Entity[]Returns array of entities.
ids(state: {}): Id[]Returns array of ids.
find(state: {}, params: { id: Id }): EntityReturns entity that matches id parameter.
count(state: {}) => numberReturns the number of entities in the Collection.
get(state: {}) => NormalizedReturns the full underlying data structure which takes the form:{ ids, entities } whereids is an array of uniqueid keys andentities is anid-based lookup map.of ids and entities.
Index creates a reducer that stores a unique set of ids. Ids can be added, removed, and toggled. An Index is perfect for
Id = string
set(payload: Id[])Takes an array of identifiers. Resets entire Index to the payload.
reset(void)Resets entire Index to empty.
toggle(payload: Id)Takes an identifier and toggles its presence in the Index.
add(payload: Id|Id[])Takes an identifier(s) and ensures its presence in the Index.
remove(payload: Id|Id[])Takes an identifier(s) and ensures its removal from the Index.
get(state: {}): Ids[]Returns the entire Index array.
includes(state: {}, { id: Id }): booleanTakes id parameter and checks whether the identifier is present in the Index, returns boolean value.
count(state: {}) => numberReturns the number of Ids in the Index.
Create new reducer definitions with thecreateDefinition function. The resulting object is a valid definition that can be used.
import{createDefinition}from'redux-definitions'constSpecialField=createDefinition({defaultState:'morty',reducers:{set:(state,{ payload})=>payload,clear:()=>undefined,},selectors:{get:(state)=>state}})export{SpecialField}
import{createReducers}from'redux-definitions'import{SpecialField}from'./specialField'const{ reducers}=createReducers({people:{rick:SpecialField}})
- Full TypeScript support
- typed action and selector maps
- typed action and selector payloads
- Support for adding additional user created actions to the REPL
- Helpful for making existing actions/reducers accessible by the REPL
- createSagas helper for creating actions that initiate sagas and add actions to REPL
- Research how to best add async saga support within definitions
- Most asynchronous business logic will involve multiple reducers and therefore should not be associated with a single reducer or definition. Business logic should use createSagas and live in its own user defined directory/file. That being said the library should support adding basic asynchronous code to definitions.
Redux Definitions is written in TypeScript. Due to the generative nature of the library fully typed actions, reducers, and selectors have proven difficult to implement. The goal is to get to the point where all actions and reducers have fully typed payloads. ✨Contributions from anyone with ideas on how to achieve this are very appreciated! Feel free to open a Githubissue or start a conversation onSpectrum with any thoughts or ideas.
- NextJS
- NextJS TypescriptHelp with this
- Create React AppHelp with this
- Create React App TypescriptHelp with this
PRs with other examples are appreciated!
Coming soon!
Please check out theContributing page to learn how to get involved.
About
🥒 Define and share reusable slices of Redux.
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.

