Skip to content

inject()

Read the value of a context provided by an ancestor. Call from inside a component() setup. Returns the context’s defaultValue when no ancestor has called provide().

function inject<T>(context: Context<T>): T
ParamTypeDescription
contextContext<T>The context object returned by createContext()

T — the nearest ancestor’s provided value, or the context’s default if none.

import { createContext, inject, component, p } from "@whisq/core";
const ThemeCtx = createContext("light");
const Child = component(() => {
const theme = inject(ThemeCtx);
return p(`Theme: ${theme}`);
});

See provide() for the producer side and Components → Context for the full pattern.