Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 39 ยท JSX and Control Flow

JSX in Solid: Expressions and Fine-Grained Updates

Learn how JSX works in SolidJS: reactive expressions, dynamic attributes and styles, spreading props, fragments, and what the compiler does to your markup.

Solid uses JSX, the same syntax many developers know from React, but it means something different. In Solid, JSX is compiled into real DOM creation code, with a tiny reactive effect for every dynamic spot. There is no virtual DOM and no diffing. Knowing that makes the rules of Solid JSX feel natural.

The <TryIt> editors on this site cannot compile JSX for Solid (that needs a build step). The examples that run use Solid's real runtime with its html tagged-template helper (solid-js/html), which behaves like JSX: ${...} plays the role of {...}. Real JSX is shown in read-only blocks. Both forms use the same reactive primitives.

Expressions in braces

Anything between braces is a JavaScript expression. Solid wraps it so that, if it reads a signal, only that spot updates.

JSX
function Profile() {
  const [name, setName] = createSignal("Ada");
  return (
    <section>
      <h2>Hello, {name()}!</h2>
      <p>Your name has {name().length} letters.</p>
    </section>
  );
}

The two braces become two tiny effects. When name changes, Solid updates two text nodes and nothing else. The Profile function does not run again.

Two independent dynamic spots

Type in the box: the console prints "Profile runs once" only at the start, yet both lines update.

Dynamic attributes, classes and styles

Attributes accept expressions too. A reactive expression in an attribute updates just that attribute.

JSX
<button
  disabled={!valid()}
  title={`Saves ${count()} items`}
  class={active() ? "btn on" : "btn"}
  style={{ color: danger() ? "crimson" : "inherit", "font-size": "1.1rem" }}
>
  Save
</button>

Points to remember:

  • Use class, not className (Solid accepts both, class is idiomatic). Likewise for on labels.
  • style takes an object with CSS property names ("font-size" or camel-case) or a string.
  • For many conditional classes, use classList (next lessons: Events, Styling and classList).
  • Boolean attributes (disabled, hidden) take booleans.
Reactive attribute, class and style

Expressions must be functions to be reactive

Solid cannot track a value, only a read. When you write {count()} the compiler turns it into a function that reads the signal. When you store the value first, the link is cut:

JSX
const [count, setCount] = createSignal(0);

const snapshot = count();      // runs once: a plain number

<p>{snapshot}</p>              // never updates
<p>{count()}</p>               // updates
<p>{count() * 2}</p>           // updates (the whole expression is tracked)

The rule: read signals where you display them, or inside a function you call there. This also explains why destructuring props is a trap; see Props and Reactivity.

Fragments, arrays and spread

  • A fragment (<>...</>) groups siblings without a wrapper element.
  • An array of nodes renders as a list. For dynamic lists use <For>, covered in For, Index and Lists.
  • Spread passes many props at once: <input {...fieldProps()} />.
  • Components start with a capital letter: <Card /> is a component, <card /> would be an element.
JSX
function Row(props) {
  return (
    <>
      <td>{props.name}</td>
      <td>{props.score}</td>
    </>
  );
}

What the compiler produces

For a static template like <div class="card"><h2>{title()}</h2></div>, the compiler emits roughly:

  1. a one-time template("<div class=card><h2></h2></div>") that is cloned with cloneNode,
  2. a reference to the h2,
  3. an insert call with an effect that sets its text when title() changes.

Creating the DOM is one clone; updating it is one text assignment. That is why Solid benchmarks so well, and it is why static markup costs nothing after the first render. You can read more in How Solid Works.

Differences from React JSX

Idea React Solid
Result of JSX Element objects, diffed Real DOM nodes, created once
Component runs On every state change Once
class attribute className class (both accepted)
Conditionals cond && <X /> <Show when={cond}>
Lists .map() with keys <For each={list}>
Events Synthetic Native DOM events

You may still write {cond() && <X />} or .map(), but they work less efficiently, because the whole expression re-runs and recreates nodes. The next lessons show the proper tools.

Common mistakes

  • Storing a signal's value in a const and displaying that.
  • Using .map() for dynamic lists, which recreates every row on change.
  • Capitalization. Lowercase tags are DOM elements, capitalized names are components.
  • Expecting JSX to be an object. Elements are real DOM nodes, so reuse the same node in two places and it moves.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Solid compiler do with an expression like {count()} in JSX?

    Each dynamic expression becomes a small reactive update; the component function is not re-run.
  2. 2. Which line will NOT update when count changes?

    Reading the signal outside the JSX stores a plain number, so there is nothing to track.
  3. 3. How do you write a conditional inline style in Solid?

    style accepts an object or a string, and it can be a reactive expression.
  4. 4. What does JSX evaluate to in Solid?

    The compiler creates actual DOM nodes once and updates them in place.

Summary

  • JSX compiles to real DOM creation plus tiny effects for each dynamic expression.
  • Read signals where you display them; do not copy their values into variables first.
  • Attributes, classes and styles can all be reactive.
  • Fragments, spread and capitalized component names work as you expect.
  • For conditionals and lists, use Solid's control-flow components.

Next: Show and Switch