Skip to content

createContext()

Create a context object that components can provide() and descendants can inject().

function createContext<T>(defaultValue: T): Context<T>
ParamTypeDescription
defaultValueTReturned by inject() when no ancestor has called provide()

Context<T> — pass to provide() and inject().

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

See Components → Context for the full pattern.