Untitled

mail@pastecode.io avatar
unknown
typescript
5 months ago
432 B
4
Indexable
/* Creates react context provider, and a hook consumer for given hook template */
export function makeContext<T>(useValue: () => T): readonly [FC<PropsWithChildren>, () => T] {
  const Context = createContext<T>({} as T)

  const provider: FC<PropsWithChildren> = ({ ...props }) => (
    <Context.Provider value={useValue()} {...props} />
  )

  const consumer = () => useContext(Context)

  return [provider, consumer]
}
Leave a Comment