npm i --save redux-bind-store
import { bindStore } from 'redux-bind-store'
// redux store
const store = createStore(reducer);
// bind to given store
const { getState, dispatch, connect } = bindStore(store);
// work with global getState and dispatch
const state = getState();
dispatch(someAction());
// connect to store changes with cache selectors
connect(state => {
isLoggedIn: isLoggedInSelector(state),
}, ({ newProps, dispatch }) => {
const { isLoggedIn } = newProps;
if (!isLoggedIn) {
// logged out case, skip
return;
}
dispatch(someActionWhenLoggedIn());
});
function bindStore(store)
store: store
- redux store
bindStore
returns an object with following properties:
getState: () => state
- redux getState method binded to the storedispatch: (action) => void
- redux dispatch method binded to the storesubscribe: (listener) => () => void
- redux subscribe method binded to the storeconnect: () => () => void
- connect to store updates
function connect(mapStateToProps, propsChangedHander)
mapStateToProps: state => object
- map the state to a props objectpropsChangedHandler: event => void
- callback for the props changed event, the handler callback will be called with event object contains:newProps: object
- currentmapStateToProps
resultprevProps: object
- previousmapStateToProps
result,null
for the first callgetState: () => state
- redux getState method binded to the storedispatch: (action) => void
- redux dispatch method binded to the store
connect
returns an unsubscribe method - the one returned by store.subscribe
- which will disconnect the propsChangedHander
when called.