Last active
August 21, 2020 10:26
-
-
Save Dodsaren/aa11b12e0bf5c35c7346c41adec670d5 to your computer and use it in GitHub Desktop.
Example of "global" hook utilizing react context
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 characters
| import React, { useContext, useState } from 'react'; | |
| const initialState = 'Jimbo'; | |
| const JimboContext = React.createContext(initialState, () => {}); | |
| export const JimboProvider = ({ children }) => { | |
| const [state, setState] = useState(initialState); | |
| return ( | |
| <JimboContext.Provider value={[state, setState]}> | |
| {children} | |
| </JimboContext.Provider> | |
| ); | |
| }; | |
| const useJimbo = () => { | |
| const [state, setState] = useContext(JimboContext); | |
| return [state, setState]; | |
| }; | |
| export default useJimbo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment