Last active
April 5, 2023 14:36
-
-
Save DevGW/f796b86607cf921cd40ca9bb1c31ba5b to your computer and use it in GitHub Desktop.
React :: CreateDataContext.js #reactnative
This file contains 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 characters
import React, { useReducer } from 'react'; | |
export default (reducer, actions, defaultValue) => { | |
const Context = React.createContext(); | |
const Provider = ({ children }) => { | |
const [state, dispatch] = useReducer(reducer, defaultValue); | |
const boundActions = {}; | |
for (let key in actions) { | |
boundActions[key] = actions[key](dispatch); | |
} | |
return ( | |
<Context.Provider value={{ state, ...boundActions }}> | |
{ children } | |
</Context.Provider> | |
); | |
}; | |
return { Context, Provider }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment