Skip to content

Latest commit

 

History

History
109 lines (87 loc) · 5.48 KB

jsx.md

File metadata and controls

109 lines (87 loc) · 5.48 KB

JSX

A .tsx file can use JSX. An element is a call of a function, the JSX factory, as tsc compiles it for "jsx": "react" (the classic runtime). There is no DOM and no React. With nothing named, the factory is h, and the default library has one: an element is a JsxElement that prints as HTML.

function Item(props: { name: string }) {
    return <li class="item">{props.name}</li>;
}

function main() {
    const names = ["one", "two & three"];
    print(<ul id="list">{names.map(n => <Item name={n} />)}</ul>);
    // <ul id="list"><li class="item">one</li><li class="item">two &amp; three</li></ul>
}

Or the factory is yours, and what an element produces is whatever it returns: an HTML string, a tree of your own nodes, widgets of a native UI library. A program's own h takes the place of the library's:

function h(tag: string, props: { id: string } | null, ...children: string[]): string {
    let s = "";
    for (const c of children) {
        s += c;
    }

    return "<" + tag + (props != null ? " id=" + props.id : "") + ">" + s + "</" + tag + ">";
}

function main() {
    print(<ul id="list"><li>one</li><li>two</li></ul>);
    // <ul id=list><li>one</li><li>two</li></ul>
}

What an element becomes

JSX The call
<div id="a">hi</div> h("div", { id: "a" }, "hi")
<Item n={1} /> h(Item, { n: 1 })
<UI.Button /> h(UI.Button, null)
<my-el />, <svg:rect /> h("my-el", null), h("svg:rect", null)
<input disabled /> h("input", { disabled: true })
<p {...props} id="z" /> h("p", { ...props, id: "z" })
<ul>{...items}</ul> h("ul", null, ...items)
<>a<b /></> h(Fragment, null, "a", h("b", null))
  • A tag that is a lowercase name or has a - is a string, and so is a:b. Any other name, or a dotted name, is the value it names: a function or class of yours, called by your factory.
  • No attributes give null. An attribute with no value is true. A string attribute has its entities decoded ("a &amp; b" is a & b); a hyphenated or namespaced name is a string key ("data-id", "xml:lang"). A spread of an object literal adds its properties one by one.
  • Text is trimmed as tsc trims it: text on one line keeps its spaces; text over several lines is trimmed line by line and the lines are joined by one space; white space that holds a line break is no child at all. Entities are decoded: the HTML 4 names (&nbsp;, &copy;, ...), &#65; and &#x1F600;. An & that is no entity stays as it is.
  • {} and {/* comment */} are no child.

The call is typed like any other: the factory's declaration decides which tags, props and children are allowed. tsc's own checking of JSX (the JSX namespace, JSX.IntrinsicElements) is not done.

Naming the factory

The factory and the fragment's tag are, first found first:

Factory Fragment tag
A comment at the top of the file /** @jsx h */ /** @jsxFrag Fragment */
The command line --jsx-factory=h --jsx-fragment-factory=Fragment
tsconfig.json, compilerOptions "jsxFactory": "h" "jsxFragmentFactory": "Fragment"
Otherwise h Fragment

tsc's default is React.createElement and React.Fragment: name them to use them. A name may be dotted (UI.h). When the factory's first name is not declared where an element is, the error says so.

h and Fragment are the program's own where it declares them (wherever in the file), else the default library's. So --jsx-factory=h with no h declared is the library's too.

The default library's h

An element is a JsxElement: its tag, its attributes as written (props, null when there are none) and its children. print(e), String(e) and `${e}` give it as HTML:

  • Text and attribute values are escaped (&, <, >, and " in a value). An attribute that is true is its name alone (<input disabled>); false, null and undefined leave it out. className and htmlFor are written class and for.
  • The children are flattened (an array, as {items.map(...)} gives, is its items), and null, undefined and booleans are left out, as React does ({shown && <b />}).
  • br, hr, img, input, meta, link and HTML's other void elements have no end tag.
  • A tag that is a function is a component, called with what its parameters take: none, the attributes, or the attributes and the children (function Card(props: { title: string }, children: any[])). Its element is the component's; anything else it returns (a string, a number) becomes a fragment of it.
  • <>...</> is a fragment: its children alone, with no tag around them.

--jsx ("jsx" in tsconfig.json) takes tsc's values: react, preserve and react-native are all the factory call above. react-jsx and react-jsxdev, the automatic runtime that imports react/jsx-runtime, are not supported and are an error at the first element; so is a file's /** @jsxRuntime automatic */.

Memory models

Under gc, rc and none an element is an ordinary call. Under -mm=own the default library's h is refused, an error at the element (its element keeps its children): declare a typed factory of your own. A factory that takes its props as any and reads their keys works there. Under -mm=own it does not, because own refuses the keys of an any. A typed factory (props of one shape, children of one type, as above) compiles under every model, -mm=own included; what it keeps of its arguments follows own's rules for any call.