Last active
December 15, 2020 16:00
-
-
Save revskill10/174ab37c5b228014e3215ca0def292a6 to your computer and use it in GitHub Desktop.
useContextState
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, { useState, createContext } from 'react'; | |
const useContextState = (context, initialState) => { | |
const state = useState(initialState); | |
const Wrapper = ({ children }) => { | |
return ( | |
<context.Provider value={state}>{children}</context.Provider> | |
) | |
} | |
return Wrapper; | |
}; | |
const ThemeContext = React.createContext(); | |
function App() { | |
const Wrapper = useContextState(ThemeContext, "dark"); | |
return ( | |
<Wrapper> | |
<Child /> | |
</Wrapper> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment