JSX in Solid — SolidJS

JSX in Solid compiles into real DOM elements: the fixed parts become one HTML template that gets cloned, and the {...} parts are filled through insert. That's…

Nadia wants a price card for every book in her shop. She doesn't draw the cards one by one. She makes one stamp holding the parts that never change (the frame, the "$" sign), then each card is simply stamped, and the parts that differ (title, price) are handwritten in the blanks.

Solid's compiler treats your JSX the same way. JSX is the way of writing HTML-like tags right inside JavaScript. If you come from React, the shape is familiar, but what it turns into is not. Your JSX is the card's design. Solid's compiler makes the stamp once. Every time the component is used, the stamp is pressed into real DOM elements, the same elements document.createElement gives you, and then the blanks are filled in.

Writing JSX

const book = { title: "The Rainbow Troops", author: "Andrea Hirata", price: 18 }

function BookCard() {
  return (
    <div class="card">
      <h2>{book.title}</h2>
      <p>{book.author}</p>
      <p style={{ color: "#b45309", "font-size": "20px" }}>
        ${book.price.toFixed(2)}
      </p>
    </div>
  )
}

The result is a card holding the title The Rainbow Troops, the author Andrea Hirata, and the price $18.00 in brown.

This lesson's exercise uses the same rules: inside the <div> that's already there, add <h1>Learning Solid</h1> and <p>Solid uses JSX.</p>. The texts are checked exactly, including the full stop at the end of the paragraph.

What is inside {...} always shows as text. If it holds HTML, for example a review from a buyer, the < and > signs show as they are, so no code runs. To put real HTML on the page, Solid has the innerHTML attribute:

import DOMPurify from "dompurify"

const review = '<b>Great!</b> <img src="x" onerror="alert(1)">'

function Review() {
  return (
    <div>
      <p>{review}</p>
      <div innerHTML={DOMPurify.sanitize(review)} />
    </div>
  )
}

JSX becomes real DOM elements

This is the code the Solid 1.9 compiler (babel-preset-solid) writes for BookCard above:

var _tmpl$ = _$template(`<div class=card><h2></h2><p></p><p style=color:#b45309;font-size:20px>$`)

function BookCard() {
  return (() => {
    var _el$ = _tmpl$(),
      _el$2 = _el$.firstChild,
      _el$3 = _el$2.nextSibling,
      _el$4 = _el$3.nextSibling
    _$insert(_el$2, () => book.title)
    _$insert(_el$3, () => book.author)
    _$insert(_el$4, () => book.price.toFixed(2), null)
    return _el$
  })()
}

So if you write const title = <h1>Nadia's Bookshop</h1> then console.log(title), what prints is a real <h1> element, an HTMLHeadingElement you could stick on the page with document.body.append(title). In React, the same JSX produces a description object ({ type: "h1", props: {...} }) that only becomes an element after React processes it.

HTML attribute names: class and for

Because Solid's compiler writes your JSX as an HTML string and direct DOM calls, attribute names follow HTML:

<label for="search">Search books</label>
<input id="search" class="search-box" />

style as an object

The style attribute can be a plain string (style="color: red"), or an object inside double curly braces: the outer pair for JavaScript, the inner pair for the object.

<p style={{ color: "#b45309", "font-size": "20px" }}>$18.00</p>

The exercise editor here uses a small model of Solid, not its real compiler, and there fontSize happens to still work. Don't go by that: in a real Solid project, use kebab-case.

⚠️ Slips that are easy to miss

TL;DR: JSX in Solid compiles into real DOM elements: the fixed parts become one HTML template that gets cloned, and the {...} parts are filled through insert. That's why <h1>...</h1> gives a real HTMLHeadingElement, not a description object as in React. Attribute names follow HTML: class (not className) and for (not htmlFor). style can be an object with quoted kebab-case keys, style={{ color: "#b45309", "font-size": "20px" }}; camelCase like fontSize is dropped by the browser. Return one outer element, close every tag, and use curly braces for JavaScript values.

⚡ Curly braces for JavaScript

Write:

const book = { title: "The Rainbow Troops", price: 18 }

function BookCard() {
  return (
    <div class="card">
      <h2>{book.title}</h2>
      <p>${book.price.toFixed(2)}</p>
    </div>
  )
}

Meaning: return gives back one outer element. {book.title} means "put the value of book.title here", and text outside the braces, like $, shows up as is.

⚡ JSX becomes real DOM elements

Guess first: you write const title = <h1>Nadia's Bookshop</h1> then console.log(title). What prints?

A real <h1> element, an HTMLHeadingElement. In React, the same JSX becomes a description object.

Meaning: Solid's compiler turns your JSX into one HTML template that is copied into real DOM elements, then the {...} parts are filled through insert. There's no virtual DOM.

⚡ Attribute names follow HTML

Write:

<label for="search">Search books</label>
<input id="search" class="search-box" />

Meaning: Because the JSX compiles to an HTML string and direct DOM calls, Solid uses class and for, not React's className and htmlFor.

This exercise fails if the word className is in your code.

⚡ style as an object

Guess first: you write style={{ fontSize: "20px" }} in a real Solid project. Does the price get bigger?

No. The browser knows no CSS property called fontSize, so it drops it. Write "font-size": "20px".

Meaning: Style keys are kebab-case in quotes, and values are full strings with units: "20px", not 20.

What you will learn