Skip to content

Instantly share code, notes, and snippets.

@revskill10
Last active December 15, 2020 16:00
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 revskill10/174ab37c5b228014e3215ca0def292a6 to your computer and use it in GitHub Desktop.
Save revskill10/174ab37c5b228014e3215ca0def292a6 to your computer and use it in GitHub Desktop.
useContextState
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