Created
October 23, 2019 15:40
-
-
Save helloncanella/fa3c3ad88af38e59f918a375c7498181 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
import React, { useState, useContext, useMemo } from "react" | |
export default function getContextManager() { | |
const Context = React.createContext() | |
function Provider(props) { | |
const [state, setState] = useState() | |
const api = useMemo(() => [state, setState], [JSON.stringify(state)]) | |
return <Context.Provider value={api}>{props.children}</Context.Provider> | |
} | |
function useContextState() { | |
return useContext(Context) | |
} | |
return { Provider, useContextState } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment