Skip to content
Ilha
Esc
↑↓navigate↵open⌘Jpreview
On this page

Context and errors

Share atom-backed values with createContext, and catch subtree failures with ErrorBoundary.

Context

Pass values through nested components without threading props. Context values are Effect atoms — reads subscribe, writes update consumers.

import { const atom: AtomFnatom, const context: <A>(ctx: IlhaContext<A>) => AtomHandle<A>
Read a context value as an atom handle. Subscribes the active fiber on read.
context
, const createContext: <A>(defaultValue: A) => IlhaContext<A>createContext } from "ilha";
const const Theme: IlhaContext<string>Theme = createContext<string>(defaultValue: string): IlhaContext<string>createContext("light"); const const Label: () => ViewLabel = () => { const const theme: AtomHandle<string, string>theme = context<string>(ctx: IlhaContext<string>): AtomHandle<string, string>
Read a context value as an atom handle. Subscribes the active fiber on read.
context
(const Theme: IlhaContext<string>Theme);
return <JSX.IntrinsicElements.span: HTMLAttributes<HTMLSpanElement>span>{const theme: AtomHandle<string, string>theme}</JSX.IntrinsicElements.span: HTMLAttributes<HTMLSpanElement>span>; }; const const App: () => ViewApp = () => { const const mode: AtomHandle<string, string>mode = atom<string>(init: string | Atom<string>, options?: AtomOptions<string> | undefined): AtomHandle<string, string> (+2 overloads)
An existing Effect atom, or a plain value. Store a function with `atom.lazy(() => fn)`; the `ilha/oxlint` rule `function-in-atom` flags `atom(fn)`.
atom
("dark");
return ( <const Theme: IlhaContext<string>Theme.IlhaContext<string>.Provider: (props: ContextProviderProps<string>) => ViewProvider ContextProviderProps<string>.value: stringvalue={
const mode: AtomHandle
() => string
mode
()}>
<JSX.IntrinsicElements.button: ButtonHTMLAttributesbutton type?: string | undefinedtype="button" EventProps<HTMLButtonElement>.onclick?: ((event: Targeted<HTMLButtonElement, MouseEvent>) => void) | undefinedonclick={() => const mode: AtomHandle<string, string>mode.AtomHandle<string, string>.set: (next: string) => voidset("light")}> Light </JSX.IntrinsicElements.button: ButtonHTMLAttributesbutton> <const Label: () => ViewLabel /> </const Theme: IlhaContext<string>Theme.IlhaContext<string>.Provider: (props: ContextProviderProps<string>) => ViewProvider> ); };
  1. createContext(defaultValue) returns a context object with a Provider.
  2. Wrap a subtree in <Theme.Provider value={…}>.
  3. Call context(Theme) in a descendant to get an AtomHandle.

Without a provider, context() returns a handle over the default value. Nested providers override outer ones. A provider re-reads value on every render and stays out of the SSR snapshot, so it never ships its value in the HTML.

Call context() during render under a fiber — same rule as reading an atom.

Error boundaries

Wrap a subtree in ErrorBoundary to paint a fallback when a child fails. Return a reset callback so the user can retry.

import { const ErrorBoundary: (props: ErrorBoundaryProps) => View
Catch subtree failures and paint a fallback. Supports reset().
ErrorBoundary
} from "ilha";
const const Boom: () => neverBoom = () => { throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error
("boom");
}; const const App: () => ViewApp = () => ( <const ErrorBoundary: (props: ErrorBoundaryProps) => View
Catch subtree failures and paint a fallback. Supports reset().
ErrorBoundary
ErrorBoundaryProps.fallback?: ErrorFallback | undefinedfallback={({ error: Errorerror, reset: () => voidreset }) => ( <JSX.IntrinsicElements.div: HTMLAttributes<HTMLDivElement>div> <JSX.IntrinsicElements.p: HTMLAttributes<HTMLParagraphElement>p>{error: Errorerror.Error.message: stringmessage}</JSX.IntrinsicElements.p: HTMLAttributes<HTMLParagraphElement>p> <JSX.IntrinsicElements.button: ButtonHTMLAttributesbutton type?: string | undefinedtype="button" EventProps<HTMLButtonElement>.onclick?: ((event: Targeted<HTMLButtonElement, MouseEvent>) => void) | undefinedonclick={reset: () => voidreset}> Retry </JSX.IntrinsicElements.button: ButtonHTMLAttributesbutton> </JSX.IntrinsicElements.div: HTMLAttributes<HTMLDivElement>div> )} ErrorBoundaryProps.onError?: ((error: Error) => void) | undefinedonError={(error: Errorerror) => var console: Consoleconsole.Console.error(...data: any[]): void (+1 overload)
Log to stderr in your terminal Appears in red
@paramdata something to display
error
(error: Errorerror)}
> <const Boom: () => neverBoom /> </const ErrorBoundary: (props: ErrorBoundaryProps) => View
Catch subtree failures and paint a fallback. Supports reset().
ErrorBoundary
>
);
Prop Role
fallback({ error, reset }) View to paint after a failure
onError(error) Optional side channel (logging)
children Protected subtree

Without a boundary, a failed child hole paints the default [data-ilha-error] view and the parent stays up. mount(host, Component, { onError }) receives that failure as an Error, so you can report it from one place.

Was this page helpful?