redux-watcher
Watch Redux state changes.
Example
const watcher = storewatcher // watch `store.getState().user`watcher // watch `store.getState().user.name` watcher // use custom compare function { console} store
Installation
npm i --save redux-watcher
You can include the script directly. (not recommended)
Or import as a module:
const ReduxWatcher =
Usage
ReduxWatcher(store)
Create a new watcher.
store
: Redux Store
ReduxWatcher.prototype.watch(selector, listener)
Watch the value of matching state.
selector
: AnArray
orString
of the path to target state. e.g.user
or['user']
forstore.getState().user
;['user', 'name']
forstore.getState().user.name
listener
: AFunction
called when target state changes. AnObject
contains following properties will be passed as the parameter:store
: Redux Store which the watcher bound to.selector
: Same as the selector above.prevState
: Previous state before store updating.currentState
: Current state of store.prevValue
: Previous value of the target state before store updating.currentValue
: Current value of the target state.
Note: Value of state will be compared deeply.
ReduxWatcher.prototype.off(selector, listener)
Stop watching the matching state.
selector
: The selector you watched before.listener
: Listener for the selector.
ReduxWatcher.select(state, selector) -> value
Return value of the state according to the selector.
state
: A state object.selector
: Selector for the target.value
: Value of the state.
ReduxWatcher.prototype.setCompareFunction(selector, isEqual)
Use custom compare function.
selector
: The selector to use custom compare function.isEqual
: AFunction
to determine if previous value equals to current value. Previous value and current value will be passed as the parameter.
ReduxWatcher.prototype.clearCompareFunction(selector)
Remove custom compare function and use the default one(deep equal).
selector
: The selector to use default compare function.
License
MIT