Last active
November 2, 2019 15:25
-
-
Save jake-daniels/d403415b10aebdfe2feea3c09beae6c0 to your computer and use it in GitHub Desktop.
Revisions
-
jake-daniels revised this gist
Feb 20, 2019 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,11 @@ function initRedux(reducer, initialState) { const actionCreator = actionMap[key] const fn = (...args) => { const action = actionCreator(...args) if (typeof action === 'function') { action(dispatch, () => appState) } else { dispatch(action) } } return { ...acc, [key]: fn } }, {}) -
jake-daniels revised this gist
Feb 19, 2019 . 1 changed file with 24 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,35 +1,35 @@ function initRedux(reducer, initialState) { const StateContext = React.createContext(null) const DispatchContext = React.createContext(null) function Provider(props) { const [appState, dispatch] = React.useReducer(reducer, initialState) return ( <StateContext.Provider value={appState}> <DispatchContext.Provider value={dispatch}> {props.children} </DispatchContext.Provider> </StateContext.Provider> ) } function useRedux(extractState, actionMap) { const appState = React.useContext(StateContext) const dispatch = React.useContext(DispatchContext) const stateExtract = extractState(appState) const actions = Object.keys(actionMap).reduce((acc, key) => { const actionCreator = actionMap[key] const fn = (...args) => { const action = actionCreator(...args) dispatch(action) } return { ...acc, [key]: fn } }, {}) return [stateExtract, actions] } return { Provider, useRedux } } -
jake-daniels revised this gist
Feb 19, 2019 . No changes.There are no files selected for viewing
-
jake-daniels revised this gist
Feb 19, 2019 . 1 changed file with 14 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,39 +1,34 @@ function initRedux(reducer, initialState) { const StateContext = React.createContext(null) const DispatchContext = React.createContext(null) function Provider(props) { const [appState, dispatch] = React.useReducer(reducer, initialState) return ( <StateContext.Provider value={appState}> <DispatchContext.Provider value={dispatch}> {props.children} </DispatchContext.Provider> </StateContext.Provider> ) } function useRedux(extractState, actionMap) { const appState = React.useContext(StateContext) const dispatch = React.useContext(DispatchContext) const stateExtract = extractState(appState) const actions = Object.keys(actionMap).reduce((acc, key) => { const actionCreator = actionMap[key] const fn = (...args) => { const action = actionCreator(...args) dispatch(action) } return { ...acc, [key]: fn } }, {}) return [stateExtract, actions] } return { Provider, useRedux } -
jake-daniels created this gist
Feb 19, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ export function initRehoox<R extends AnyReducer, S extends React.ReducerState<R>>(reducer: R, initialState: S) { const StateContext = React.createContext<S | null>(null) const DispatchContext = React.createContext<React.Dispatch<React.ReducerAction<R>> | null>(null) function Provider(props: { children: JSX.Element }) { const [appState, dispatch] = React.useReducer(reducer, initialState) return ( <StateContext.Provider value={appState}> <DispatchContext.Provider value={dispatch}>{props.children}</DispatchContext.Provider> </StateContext.Provider> ) } function useRedux<StateExtract, ActionMap extends DispatchMap<S>>( extractState: (state: S) => StateExtract, actionMap: ActionMap, ) { const appState = React.useContext(StateContext) as S const dispatch = React.useContext(DispatchContext) as React.Dispatch<React.ReducerAction<R>> const stateExtract = extractState(appState) const actions = Object.keys(actionMap).reduce((acc: object, key: string) => { const actionCreator = actionMap[key] const fn = (...args: ArgsTypes<typeof actionCreator>) => { const action = actionCreator(...args) if (typeof action === 'function') { action(dispatch as Dispatch<S, Action<any>>, () => appState) } else { dispatch(action as React.ReducerAction<R>) } } return { ...acc, [key]: fn } }, {}) return [stateExtract, actions] as [StateExtract, AppActions<ActionMap>] } return { Provider, useRedux } }