Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Created October 10, 2019 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyraspopov/d5c9a84c4a31a870c5cd864fbf3c388a to your computer and use it in GitHub Desktop.
Save alexeyraspopov/d5c9a84c4a31a870c5cd864fbf3c388a to your computer and use it in GitHub Desktop.
// MyContext.js
let MyContext = createContext();
export function MyDataProvider({ children }) {
let state = useState(null);
return <MyContext.Provider value={state} children={children} />
}
export function useData() {
let [data] = useContext(MyContext);
return data;
}
export function useDataUpdateA() {
let [,dispatch] = useContext(MyContext);
return (payload) => dispatch({ type: 'UpdateA', payload });
}
export function useDataUpdateB() {
let [,dispatch] = useContext(MyContext);
return (payload) => dispatch({ type: 'UpdateB', payload });
}
// MyComponent.js
import { MyDataProvider, useData, useDataUpdateA } from './MyContext.js';
export default function MyComponent() {
let data = useData();
let updateThing = useDataUpdateA();
return ...;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment