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 }
}
