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
knot
const url = "https://example.com/image.png";const element = <img src=url></img>;const selfClosing = <img src=url />;
knot
const url = "https://example.com/image.png";const element = <img src=url></img>;const selfClosing = <img src=url />;
tsx
const url = "https://example.com/image.png";const element = <img src={url}></img>;const selfClosing = <img src={url} />;
tsx
const 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
knot
const stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled=true />;const numericAttr = <input tabindex=(123 + 456 + 789) />;
knot
const stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled=true />;const numericAttr = <input tabindex=(123 + 456 + 789) />;
tsx
const stringAttr = <input value="foo bar" />;const booleanAttr = <input disabled={true} />;const numericAttr = <input tabindex={123 + 456 + 789} />;
tsx
const 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
knot
const name = "foo";const counter = 10;const element = <Widget name counter />;
knot
const name = "foo";const counter = 10;const element = <Widget name counter />;
tsx
const name = "foo";const counter = 10;const element = <Widget name={name} counter={counter} />;
tsx
const 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
knot
const element = <input disabled=true />;
knot
const element = <input disabled=true />;
tsx
const element = <input disabled />;
tsx
const 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
knot
func hello -> "Hello, world!";const inline = <h1>{hello()}</h1>;
knot
func hello -> "Hello, world!";const inline = <h1>{hello()}</h1>;
tsx
function hello() {return "Hello, world!";}const inline = <h1>{hello()}</h1>;
tsx
function 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
knot
const inline = <h1>Hello, world!</h1>;
knot
const inline = <h1>Hello, world!</h1>;
tsx
const inline = <h1>Hello, world!</h1>;
tsx
const 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
knot
const nested =<section><h1>Welcome</h1><main>👋</main></section>;
knot
const nested =<section><h1>Welcome</h1><main>👋</main></section>;
tsx
const nested = (<section><h1>Welcome</h1><main>👋</main></section>);
tsx
const nested = (<section><h1>Welcome</h1><main>👋</main></section>);
Mixed Content
Elements can also contain mixed content of all the types listed above.
- Knot
- TypeScript
knot
const mixed =<div>Welcome!{123}<main>🗑</main></div>;
knot
const mixed =<div>Welcome!{123}<main>🗑</main></div>;
tsx
const mixed = (<div>Welcome!{123}<main>🗑</main></div>);
tsx
const mixed = (<div>Welcome!{123}<main>🗑</main></div>);