RRH
What's this?
Simple https://reactjs.org/docs/hooks-overview.html' of https://github.com/reduxjs/react-redux
Install
npm
npm install rrh --save
yarn
yarn add rrh
How to use
Provider
;;;; const Component = { const Provider = ; return <Provider><Child></Provider>}
connect
;; const Child = { const selected = ; return <div> selectedcount <button onClick=selectedincrement>INC</button> <button onClick=selecteddecrement>DEC</button> </div> }
API
useProvider
types
State, Ext = , StateExt = Provider```__overview__ Return redux store Provider Component that has store inside. No props needed.All arguments are same as redux's `createStore`. Store and provider is initialized once per process, if useProvider called.So if you want to create new store and Provider, you need to create new hooks instance. See below. ### useSelector __types__ ```typescriptHandlers, ExtractedState, Props, State props: React.PropsWithChildren<Props>, mapDispatchToProps:Handlers, mapStateToProps:ExtractedState, deps?: any | any Handlers & ExtractedState
overview
useSelector behave like redux's connect(config)(Component)
, but like https://github.com/reduxjs/reselect,
we will check dependencies are updated or not, by using useMemo
.
So if deps
is updated, mapDispatchToProps
and mapStateToProps
will be called.
If you want to specify deps from state, do like below.
or if you want to use array deps just like other hooks.
useDispatchToProps
types
Props, Handlers props: Props, mapDispatchToProps:Handlers, deps?: any Handlers
overview
useDispatchToProps
can partialy call mapDispatchToProps to the store state.
useStateToProps
types
<Props, State, ExtractedState>(
props: Props,
mapStateToProps: (state: State, ownProps: Props) => ExtractedState,
deps?: any[] | ((state: State) => any[])
) => ExtractedState
overview
useStateToProps
can partialy call mapStateToProps to the store state.
If you want to specify deps from state, do like below.
or if you want to use array deps just like other hooks.
useDispatch
types
Dispatch
overview
useDispatch
is simply return store.dispatch
.
const dispatch = ;
useStore
types
Store
overview
useStore
is simply return store
.
const store = console;
Muliple store.
If you want to create muliple store, you need to create new hooks instance by using generateReduxHooks
.
generateReduxHooks
types
Hooks
overview
generateReduxHooks
is create new hooks instance.
All hooks returned from generateReduxHooks
is not initialized, so if you call useProvider
,
new store
and Provider
will be created.
Between hooks instances, store is not shared.
initialization timing
generateReduxHooks
accepts RRHStoreInitilizationTiming
, this options behave like below.
type | description |
---|---|
EACH_MOUNT | Store and Provider will be initialized each componentDidMunt |
ONCE_PER_FACTORY | Store and Provider will be initialized only first useProvider call. |
default hooks
All default hooks that expored from 'rrh' package are initialized by RRHStoreInitilizationTiming.ONCE_PER_FACTORY
.