ILHA001: Primitive with no active component
atom(), watch(), when(), or context() ran outside a component that mount() or renderToString() started.
Primitives bind to the component that is rendering. Outside a render, no component is active, so Ilha throws ILHA001.
Why it happens
- You call
atom()at module top level. - You call a primitive from a timer, an event handler, or a promise callback that runs after the render.
- You call a component function yourself instead of rendering it through
mount()orrenderToString().
Bad
import { atom } from "ilha";
const count = atom(0);
const Counter = () => <p>{count}</p>;
Fixed
Create the atom inside the component, at the top level of its body:
import { const atom: AtomFnatom, const mount: (el: Element, fn: Component, opts?: MountOptions) => (() => void)mount } from "ilha";
const const Counter: () => ViewCounter = () => {
const const count: AtomHandle<number>count = atom<number>(init: number | Atom<number>, options?: AtomOptions<number> | undefined): AtomHandle<number> (+1 overload)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(0);
return (
<JSX.IntrinsicElements.button: ButtonHTMLAttributesbutton type?: string | undefinedtype="button" EventProps<HTMLButtonElement>.onclick?: ((event: Targeted<HTMLButtonElement, MouseEvent>) => void) | undefinedonclick={() => const count: AtomHandle<number>count.AtomHandle<number>.update: (f: (current: number) => number) => voidupdate((n: numbern) => n: numbern + 1)}>
{const count: AtomHandle<number>count}
</JSX.IntrinsicElements.button: ButtonHTMLAttributesbutton>
);
};
function mount(el: Element, fn: Component, opts?: MountOptions): (() => void)mount(var document: Document**`window.document`** returns a reference to the document contained in the window.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/document)document.Document.body: HTMLElementThe **`Document.body`** property represents the <body> or <frameset> node of the current document, or null if no such element exists.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/body)body, const Counter: () => ViewCounter);
Share state between components with createContext() rather than a module-level atom.