Skip to content

Instantly share code, notes, and snippets.

@helloncanella
Created October 23, 2019 15:40
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 helloncanella/fa3c3ad88af38e59f918a375c7498181 to your computer and use it in GitHub Desktop.
Save helloncanella/fa3c3ad88af38e59f918a375c7498181 to your computer and use it in GitHub Desktop.
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