add useDispatch hook

This commit is contained in:
Alfred Melch 2020-02-01 18:06:29 +01:00
parent 7f2201c945
commit 4b98f6f654

View File

@ -21,13 +21,15 @@ export const usePotentReducer = options => {
*/ */
export const createStore = options => { export const createStore = options => {
const context = createContext(null) const context = createContext(null)
const { Consumer } = context
const Provider = ({ children }) => { const Provider = ({ children }) => {
const store = usePotentReducer(options) const store = usePotentReducer(options)
return React.createElement(context.Provider, { value: store }, children) return React.createElement(context.Provider, { value: store }, children)
} }
const useStore = () => useContext(context) const useStore = () => useContext(context)
const useSelector = selector => selector(useContext(context)[0]) const useSelector = selector => selector(useContext(context)[0])
return { Provider, Consumer: context.Consumer, useStore, useSelector } const useDispatch = () => useContext(context)[1]
return { Provider, Consumer, useStore, useSelector, useDispatch }
} }
/** Turn a reducer definition object to a function /** Turn a reducer definition object to a function