Create component
Write a component — a function, async function, or generator that returns JSX.
A component is a function that returns a view. Sync functions return JSX. Async functions can await. Generators yield streams or yield* Effects and when.
const const Hello: () => ViewHello = () => <IntrinsicElements[string]: anyp>Hello</IntrinsicElements[string]: anyp>;
Call Hello from another component as a nested function.
Call mount or renderToString with Hello when it is the root.
Props
Pass props as the first argument:
const const Greeting: ({ name }: {
name: string;
}) => View
Greeting = ({ name: stringname }: { name: stringname: string }) => (
<IntrinsicElements[string]: anyp>Hello, {name: stringname}</IntrinsicElements[string]: anyp>
);
When you mount a root that needs props, close over them:
import { function mount(el: Element, fn: Component, opts?: MountOptions): () => voidmount } from "ilha";
const const Greeting: ({ name }: {
name: string;
}) => View
Greeting = ({ name: stringname }: { name: stringname: string }) => (
<IntrinsicElements[string]: anyp>Hello, {name: stringname}</IntrinsicElements[string]: anyp>
);
const const root: HTMLElement | nullroot = 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.getElementById(elementId: string): HTMLElement | nullThe **`getElementById()`** method of the Document interface returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.getElementById("app");
if (const root: HTMLElement | nullroot) function mount(el: Element, fn: Component, opts?: MountOptions): () => voidmount(const root: HTMLElementroot, () => const Greeting: ({ name }: {
name: string;
}) => View
Greeting({ name: stringname: "Ada" }));
Nested functions
A nested function shares the parent fiber. Atoms you declare there still belong to the parent. Keep call order stable across reruns.
import { const atom: AtomFnatom } from "ilha";
const const Label: (props: Record<string, unknown>) => ViewLabel = (props: Record<string, unknown>props: type Record<K extends keyof any, T> = { [P in K]: T; }Construct a type with a set of properties K of type TRecord<string, unknown>) => (
<IntrinsicElements[string]: anyspan>{var String: StringConstructor
(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.String(props: Record<string, unknown>props.unknowntext)}</IntrinsicElements[string]: anyspan>
);
const const Card: () => ViewCard = () => {
const const title: AtomHandle<string>title = atom<string>(init: string | Atom<string> | Effect<string, any, any> | Stream<string, any, any>): AtomHandle<string>atom("Inbox");
return (
<IntrinsicElements[string]: anysection>
<const Label: (props: Record<string, unknown>) => ViewLabel text: stringtext={const title: AtomHandle
() => string
title()} />
</IntrinsicElements[string]: anysection>
);
};
Async component
const const Page: () => Promise<View>Page = async () => {
const const title: stringtitle = await var Promise: PromiseConstructorRepresents the completion of an asynchronous operationPromise.PromiseConstructor.resolve<string>(value: string): Promise<string> (+2 overloads)Creates a new resolved promise for the provided value.resolve("Home");
return <IntrinsicElements[string]: anyh1>{const title: stringtitle}</IntrinsicElements[string]: anyh1>;
};
renderToString waits until the promise settles and later work goes idle.
Generator component
Use a generator when you yield a Stream of views or yield* when(...). See Streams.
JSX config
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "ilha"
}
}
Markup, events, and lists are in Templating.