Skip to main content
Module

x/keywork/mod.ts>ReactUtils.createContextAndNamedHook

A library for building V8 Isolate web apps on Cloudflare Workers, Deno, and Node.JS
Go to Latest
function ReactUtils.createContextAndNamedHook
Re-export
import { ReactUtils } from "https://deno.land/x/keywork@v6.0.1/mod.ts";
const { createContextAndNamedHook } = ReactUtils;

A convenience wrapper around React.createContext that creates a context and hook for the child components within a Provider's context.

Creating a named context Provider with a computed value.

const [WidgetsContext, useWidgetsContext] = createNamedContextHook<WidgetsProvider>()
// Note that the `WidgetsContext` is not exported if the provider needs additional logic.
export { useWidgetsContext }

// Instead, a component is declared to pass down a computed value.
export const WidgetProvider: React.FC<WidgetProviderProps> = ({children}) => {
  // Always use a memoized value to prevent rerenders.
  // Some combination of props go here...
  const value = useMemo<WidgetsProvider>(() => ({}), [dep1, dep2, depN])

  return <WidgetsContext.Provider value={value}>{children}</WidgetsContext.Provider>
}

Parameters

optional
defaultValue: T | undefined = [UNSUPPORTED]
optional
displayName: string

Returns

readonly [React.Context<T | undefined>, <V = T>() => NonNullable<V>]

contextTuple