liveBook · Manning

JSX

As explained in Web Component Development with Modern Libraries and Tooling

A JavaScript syntax extension that represents user-interface markup within source code and is transformed by a compiler.

JSX lets a component describe its rendered HTML with markup inside JavaScript or TypeScript code. For example, a radio-group component can return a fieldset containing a legend and radio buttons from its render method. Browsers do not understand JSX directly, so Stencil compiles its .tsx components into web components. section 6.7 section 6.1

A small render method

render() { return <fieldset><legend>{this.legend}</legend></fieldset>; } The braces insert the component’s legend value into the markup. A fuller radio group maps values to inputs and connects their change handler. section 6.7

Listing 6.6 radio-group.tsx
  render() {
    const labelArray = this.labels.split(',').map(value => value.trim());
    const valueArray = this.values.split(',').map(value => value.trim());
    return (
      <fieldset>
        <legend>{this.legend}</legend>
        <slot name="before"></slot>
        <div>
          {valueArray.map((v, index) => (
            <div>
              <input
                type="radio"
                id={v}
                name={this.name}
                value={v} checked={v === this.value}
                onChange={this.handleChange}
              />
              <label htmlFor={v}>{labelArray[index]}</label>
            </div>
          ))}
        </div>
        <slot name="after"></slot>
      </fieldset>
    );
  }

The complete render method shows labels, radio inputs, slots, and an event handler together in JSX. section 6.7

JSX is a syntax choice, not a requirement for web components. Stencil uses it instead of tagged template literals and therefore requires a build step. When using custom elements in a React TypeScript app, JSX declarations can specify their accepted attributes and optional element references. In React 19, a JSX prop becomes an element property if a matching property exists; otherwise React sets an attribute. section 6.1 section 9.4

Listing 9.12 React jsx.d.ts
import "react";

declare module "react" {
  namespace JSX {
    interface IntrinsicElements {
      "hello-world": {
        name?: string;
        ref?: any;
      };
      "radio-group": {
        name: string;
        labels: string;
        ref?: any;
        value: string;
        values: string;
        onChange?: (event: Event) => void;
      };
    }
  }
}

The type declaration shows how a React TypeScript app makes two custom elements available in JSX. section 9.4

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Shows a worked render method and React declarations, but stops short of compiler transformation internals.
Examples
Uses a radio-group render method with mapped inputs and a React declaration for radio-group and hello-world elements.
What you can do after
Apply it via Stencil render methods and React TypeScript declarations for custom elements.
How it connects
Connects JSX to web components, tagged template literals, TypeScript declarations, and React custom-element properties.

Also explained in

Compare how 9 books explain JSX →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.