liveBook · Manning

Rendering

As explained in Web Component Development with Modern Libraries and Tooling

A component method that returns the JSX or template describing the component's rendered content.

A component’s render method describes the content it displays. In Lit, it returns an HTML template; in Stencil, it returns JSX. A plain JavaScript component can instead use a render method that directly updates its shadow DOM. section 5.2 section 6.7 section 9.1

A greeting that changes

A Lit greeting returns `html` containing `<p>Hello, ${this.name}!</p>`. The expression marks the changing part. When the reactive `name` property changes, Lit calls render again, recomputes dynamic parts, and updates only those whose values changed. section 5.2

Listing 5.1 hello-world.ts
import { css, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators.js";

@customElement("hello-world")
export class HelloWorld extends LitElement {
  static styles = css` #1
    :host {
       color: blue;
    }
  `;

  @property() name = "World";

  render() {
    return html`<p>Hello, ${this.name}!</p>`;
  }
}

See the complete greeting component: its reactive property supplies the value used in the render method. section 5.2

Rendering can also assemble larger structures. A Lit sortable table calls helper methods for headings and rows from its template. A Stencil radio group maps values to JSX inputs and labels, setting each input’s checked state from the current value. Its change handler updates that value. section 5.9 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>
    );
  }

Compare this JSX render method’s radio inputs, checked values, change handler, and named slots. section 6.7

The main difference is who updates the DOM. Lit tracks changing template parts. Stencil compares successive virtual DOM versions and updates differing nodes; direct DOM manipulation in its component methods can conflict with that process. In the plain JavaScript radio group, render clones a template, inserts button HTML, and replaces the shadow DOM contents explicitly. section 5.2 section 6.5 section 9.1

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works through render methods in Lit, Stencil, and plain JavaScript, including template expressions, JSX, helper methods, and DOM update behavior.
Examples
Uses a greeting, sortable table, and radio group with checked inputs, labels, and slots.
What it assumes
Uses HTML and JavaScript component classes while introducing reactive properties through a small greeting component.
What you can do after
Build it from scratch with a plain JavaScript render method, or apply it via Lit templates and Stencil JSX.
How it connects
Connects rendering to reactive properties, shadow DOM, JSX, event handling, lifecycle hooks, and virtual DOM updates.

Also explained in

Compare how 6 books explain rendering →

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.