redux-way
A small, simple and immutable model to manage data in your Redux store.
Motivation
After multiple project with react and redux, the file structure begin unmaintainable, constants on one side, reducers on another. I tried to put everything in a file but I end up with a large file. So I created for my needs, a bookstore allowing me to have maintainable and clear code I was inspired by react-redux and react-ORM
Installation
npm install --save redux-way
Usage
Declare your model
; const INCREMENT = 'INCREMENT';const DECREMENT = 'DECREMENT';const RESET = 'RESET'; // Used to resolve all related store static modelName = 'counter'; // Initial state static state = 0; // Differents actions related to the model static actions = type: INCREMENT type: DECREMENT type: RESET ; // Reducer linked by constants reducer = INCREMENT: {model} DECREMENT: {model} RESET: {model} ;
Register your model and create store
;;; const register = ; // Register your modelsregister; const store = ;
Connect your composant
; ;; Component { const counter decrement increment reset = thisprops; return <div> counter <br /> <button onClick=decrement>Decrement</button> <button onClick=increment>Incremente</button> <button onClick=reset>Reset</button> </div> } // Same api as react-reduxconst mapStateToProps = { return counter: statecounter } const mapDispatchToProps = increment: CounterModelactionsincrement decrement: CounterModelactionsdecrement reset: CounterModelactionsreset mapStateToProps mapDispatchToPropsCounter
Use redux-saga
;; ; const sagaMiddleware = const register = ; // Register your modelsregister; const store = ; // register sagaMiddleware, launch after store has been createdregister;
; const INCREMENT = 'INCREMENT';const ASYNC_INCREMENT = 'ASYNC_INCREMENT';const DECREMENT = 'DECREMENT';const RESET = 'RESET'; // Used to resolve all related store static modelName = 'counter'; // Initial state static state = 0; // Differents actions related to the model static actions = type: INCREMENT type: ASYNC_INCREMENT type: DECREMENT type: RESET ; // launch by sagaMiddleware.run { } { ; } // Reducer linked by constants reducer = INCREMENT: {model} DECREMENT: {model} RESET: {model} ;
Api
Model
update(mergeObj)
: update the state by merging mergeObj. Returns undefined