Created
October 10, 2019 15:25
-
-
Save alexeyraspopov/d5c9a84c4a31a870c5cd864fbf3c388a to your computer and use it in GitHub Desktop.
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
// 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