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.
- One outer element.
returngives back one element, here a<div>. If you don't want an extradiv, wrap with a fragment<>...</>. - Curly braces for JavaScript.
{book.title}means "put the value ofbook.titlehere". Any expression works, for example{book.price.toFixed(2)}which turns18into18.00. - Every tag is closed. A tag with no content is written with a slash:
<input />,<img />,<br />. - Text outside the braces is written as is. The
$before{...}shows up exactly like that.
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>
)
}
<p>{review}</p>shows the review as text, including<b>and<img ...>.innerHTML={review}without a cleaner adds that<img>as an element, and the browser runsonerrorwhen the image fails to load. That is an XSS attack: someone else runs JavaScript on your shop's page.DOMPurify.sanitize(from the DOMPurify library,npm install dompurify) removesonerror, so what is added is<b>Great!</b> <img src="x">. UseinnerHTMLonly for HTML you wrote yourself, or HTML that has been cleaned.
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$
})()
}
- Every fixed part (tags,
class,style, the text$) is gathered into one HTML string in_$template. That's the stamp. _tmpl$()copies that string into real DOM elements._el$2,_el$3,_el$4are the<h2>and the two<p>inside it._$insert(...)fills the blanks from the expressions in braces.- The function returns
_el$, anHTMLDivElement.
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" />
classgives a CSS class, just like in HTML. React usesclassName, because React's JSX maps to the element's JavaScript properties (el.className), not to HTML attributes.foron a<label>ties the label to the input with the sameid. React writes ithtmlFor.- Solid 1.x still accepts
classNameandhtmlForas old names, but Solid's docs and the exercises here useclassandfor. This exercise rejects the wordclassNameanywhere in your code (comments don't count). - Events are written with an
onprefix:onClick,onInput. The details come in the Event Handling lesson.
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>
- Keys are written as in CSS, kebab-case:
"font-size","background-color". Because of the dash, the key needs quotes. - Values are full strings with units:
"20px", not20. fontSize(camelCase, React style) doesn't work in Solid. The compiler writes it as is,fontSize:20px, into the template, the browser knows no CSS property calledfontSize, and drops it. The price stays at its normal size.
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
classNameout of React habit. In Solid writeclass. This exercise fails if the wordclassNameis in your code, even when the page looks right.- camelCase style keys.
{{ fontSize: "20px" }}is dropped by the browser in real Solid. Use{{ "font-size": "20px" }}. - Two outer elements with no wrapper.
return <h1>...</h1><p>...</p>is an error. Wrap them in a<div>or<>...</>. - A text that's slightly off.
Solid uses JSXwithout the full stop isn'tSolid uses JSX.. Copy the text from the brief exactly.
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.