Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/keywork/react/functions/mod.ts>createContextAndNamedHook

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

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