Skip to content

Instantly share code, notes, and snippets.

@DevGW
Last active April 5, 2023 14:36
Show Gist options
  • Save DevGW/f796b86607cf921cd40ca9bb1c31ba5b to your computer and use it in GitHub Desktop.
Save DevGW/f796b86607cf921cd40ca9bb1c31ba5b to your computer and use it in GitHub Desktop.
React :: CreateDataContext.js #reactnative
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