Table of Contents 📋
Installation 📦
Redux Detector requires Redux 3.1.0 or later.
npm install --save redux-detector
This assumes that you’re using npm package manager with a module bundler like Webpack to consume ES6 or CommonJS modules.
To enable Redux Detector, use createDetectorEnhancer
:
;;;; const store = ;
Concept 💡
Redux Detector enhancer allows you to detect state changes in the Redux.
The Detector
is a pure function which accepts previous and next state and returns something for given states transition.
;
The Actions Detector
is a Detector
which returns action, list of actions or nothing.
Returned actions are automatically dispatched by the enhancer.
;; ;
Another type of the detector is the Condition Detector
which returns boolean values.
; ;
These two types of detectors have different responsibility:
Condition Detectors
describes a condition that we want to detectActions Detectors
describes which action we want to dispatch
Thanks to its functional nature and purity, detectors are easy to test. They don't break Single Source of Truth principle as the input is only previous and next state.
Basics 👈
Let's start simply - implement a condition detector that checks if number of login attempts exceeded 3.
;
We can make above example more generic - prevState.attempts <= 3
is the same as !(prevState.attempts > 3)
.
That means that we check if some condition is not truthy for the previous state but is truthy for the next state.
This kind of transition can be handled by the changedToTruthy
function.
; ;
Redux Detector library provides other useful functions to model condition detectors - please check the API documentation to learn more.
The next step is to use an action detector to dispatch an action when the limit became exceeded.
To do so, we will use composeIf
function.
;; ;
The createDetectorEnhancer
function accepts only one detector, so we have to compose all
detectors to the one rootDetector
.
;;// other detectors...; ;
And that's all - redux-detector
will dispatch blockUser()
when login attempts exceeded 3 🎉
API reference 📖
For more detailed documentation, please check API reference.
Code splitting ✂️
Redux Detector provides replaceDetector
method on DetectableStore
interface (store created by Redux Detector). It's similar to
replaceReducer
- it changes detector and dispatches { type: '@@detector/INIT' }
.
Typings 📐
If you are using TypeScript, typings are provided in the npm package. This library doesn't provide Flow typings.
License
MIT