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





