Elements
This expression is used to describe an element in the application's render tree. The syntax is very similar to React JSX.
Most views will return an element to be rendered.
Definition
Element primitives can be constructed outside of views though they will not be connected to render tree until they are used within a rendered view. If an element has no children then it can be written as self-closing.
- Knot
- TypeScript
knotconst url = "https://example.com/image.png";const element = <img src=url></img>;const selfClosing = <img src=url />;
knotconst url = "https://example.com/image.png";const element = <img src=url></img>;const selfClosing = <img src=url />;
tsxconst url = "https://example.com/image.png";const element = <img src={url}></img>;const selfClosing = <img src={url} />;
tsxconst url = "https://example.com/image.png";const element = <img src={url}></img>;const selfClosing = <img src={url} />;
Adding Parentheses
Most property expressions do not require explicitly adding parentheses, but for complex expressions it can help to better indicate the end of an expression.
- Knot
- TypeScript
knotconst stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled=true />;const numericAttr = <input tabindex=(123 + 456 + 789) />;
knotconst stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled=true />;const numericAttr = <input tabindex=(123 + 456 + 789) />;
tsxconst stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled={true} />;const numericAttr = <input tabindex={123 + 456 + 789} />;
tsxconst stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled={true} />;const numericAttr = <input tabindex={123 + 456 + 789} />;
Attribute Punning
You can easily provide the attributes of an element from a variable with a matching name in the same scope.
- Knot
- TypeScript
knotconst name = "foo";const counter = 10;const element = <Widget name counter />;
knotconst name = "foo";const counter = 10;const element = <Widget name counter />;
tsxconst name = "foo";const counter = 10;const element = <Widget name={name} counter={counter} />;
tsxconst name = "foo";const counter = 10;const element = <Widget name={name} counter={counter} />;
Note that this means unlike JSX an attribute without a value does not have a value of true.
- Knot
- TypeScript
knotconst element = <input disabled=true />;
knotconst element = <input disabled=true />;
tsxconst element = <input disabled />;
tsxconst element = <input disabled />;
Inline Expressions
Expressions can be added inline by wrapping it in braces ({ }) to provide the inner content of an element.
- Knot
- TypeScript
knotfunc hello -> "Hello, world!";const inline = <h1>{hello()}</h1>;
knotfunc hello -> "Hello, world!";const inline = <h1>{hello()}</h1>;
tsxfunction hello() {return "Hello, world!";}const inline = <h1>{hello()}</h1>;
tsxfunction hello() {return "Hello, world!";}const inline = <h1>{hello()}</h1>;
Text Content
Simple textual content can be written inline to provide the inner content of an element.
- Knot
- TypeScript
knotconst inline = <h1>Hello, world!</h1>;
knotconst inline = <h1>Hello, world!</h1>;
tsxconst inline = <h1>Hello, world!</h1>;
tsxconst inline = <h1>Hello, world!</h1>;
Nested Elements
Compose together elements in a way familiar to anyone who has used JSX, XML or HTML.
- Knot
- TypeScript
knotconst nested =<section><h1>Welcome</h1><main>👋</main></section>;
knotconst nested =<section><h1>Welcome</h1><main>👋</main></section>;
tsxconst nested = (<section><h1>Welcome</h1><main>👋</main></section>);
tsxconst nested = (<section><h1>Welcome</h1><main>👋</main></section>);
Mixed Content
Elements can also contain mixed content of all the types listed above.
- Knot
- TypeScript
knotconst mixed =<div>Welcome!{123}<main>🗑</main></div>;
knotconst mixed =<div>Welcome!{123}<main>🗑</main></div>;
tsxconst mixed = (<div>Welcome!{123}<main>🗑</main></div>);
tsxconst mixed = (<div>Welcome!{123}<main>🗑</main></div>);