Dispatch reducers
Repatch is just a simplified Redux, that let you create actions more briefly by dispatching reducers directly.
store;
In this terminology, an action is a function that returns a reducer:
const increment = ...state counter: statecounter + amount; store;
Motivation
Redux has verbose action management. The most of redux projects do not need sctrict action administration. Action types, action creators and the reducer's action handlers are mutually assigned to each other. Repatch's purpose is creating actions briefly.
The simplest way to keep the immutable action controlled dataflow and define actions briefly is dispatching pure functions (as reducers) to the store.
Redux
Comparison withRepatch is
- less verbose
- smaller (the minified version is less than 1 KB)
- faster
than Redux.
Redux
Working withIf you have to keep the official Redux in your project, then you can use the redux-repatch or redux-repatch-creator enhancers.
API Reference
Examples
Articles
Repatch - the simplified Redux
Installation
npm install --save repatch
How to use
ES6
; const store = initialState;
CommonJS
const Store = Store;
UMD
or the minified bundle:
and
const Store = RepatchStore;const thunk = Repatchthunk;
react-redux
Compatibility withRepatch's interface is very similar to Redux, therefore you can use with react-redux.
const unsubscribe = store; store; ;
TODO app in brief
const store = ; const addTodo = ...todos text checked: false ; const checkTodo = todos; const editTodo = todos; const removeTodo = todos;
Sub-reducers
We do not need to reduce always the whole state of the store. Repatch also offers a way to combine sub-reducers, those describe a deeply nested property in the state. We just define a helper function that takes a nested reducer as argument, and returns a reducer that reduces the whole state:
const reduceFoo = ...state bar: ...statebar foo: ;
Using that we can define easily an action, that sets an x
property in the foo
object:
const setX = ;
Middlewares
A repatch middleware takes the store
instance, a next
function and the previous reducer
. The middleware can provide a new reducer via the next
function.
Middleware: Store -> Next -> Reducer -> any
Use the addMiddleware
method to chaining middlewares:
const store = initialState ;
Middleware example
This simple logger middleware logs the current- and the next state:
const logger = { const state = store const nextState = console return } const store = initialState
Async actions
The thunk
middleware is useful for handling async actions similar to redux-thunk.
; const store = initialState;
In thunk async actions reducer returns a function (delegate):
const updateUser = async { try const editedUserId = editedUser; ; await api; await ; catch error finally ; };
It is possible to embed async actions within each other too and awaiting their resolving:
await ;
Injecting extra argument
It is possible to inject extra arguments into async actions:
;;; const store = initialState ;
Then you can access these arguments in your delegates:
const updateUser = async { // ... }
This way you can keep your async actions independently from outer instances or side-effects. This practice is useful for testing.
Testing
Sync actions
Testing a reducer is easy:
;; // ... ;
Async actions
For async action tests you need to instantiate the Store
and provide mocked extra arguments.
;;; const mockUsers = username: 'john' ;const mockApi = Promise // ... ;