The Rainbow Troops is down to one copy on the shelf. The last buyer presses Buy 1, the stock hits 0, and that button should disappear, swapped for an Out of stock sign. The label beside it also has to pick one of three: available, running low, or sold out. Nadia checks her rules from top to bottom ("sold out?", "running low?", "available?") and hangs the sign from the first rule that fits.
Choosing what shows based on a condition like this is called conditional rendering. Solid has two built-in components for it: <Show> for "if this, show this, otherwise show that", and <Switch> holding several <Match> for more than two choices.
Show: when and fallback
import { createSignal, Show } from "solid-js"
function App() {
const [loggedIn, setLoggedIn] = createSignal(false)
return (
<div>
<Show when={loggedIn()} fallback={<p>Please log in</p>}>
<p>Welcome!</p>
</Show>
<button onClick={() => setLoggedIn(v => !v)}>
{loggedIn() ? "Logout" : "Login"}
</button>
</div>
)
}
| Event | loggedIn() |
What shows |
|---|---|---|
| Page opens | false |
<p>Please log in</p> |
| One click | true |
<p>Welcome!</p> |
| Another click | false |
<p>Please log in</p> |
Showis imported from"solid-js", just likecreateSignal.when={loggedIn()}is the condition. The signal is called with parentheses, so Solid watches it.- What sits between
<Show>and</Show>shows whilewhenis truthy (a value counted as true:true, any number but 0, non-empty text, an object). fallback={...}shows whilewhenis falsy (false,0,"",null,undefined). Without afallback, nothing shows.setLoggedIn(v => !v)flips the value, so the button swaps the two back and forth. That's the first exercise.
A Buy button or "Out of stock"
The card for The Rainbow Troops with a stock of 4. Its buy button is made by a small component that prints to the console every time it runs:
import { createSignal, Show, Switch, Match } from "solid-js"
function BuyButton(props) {
console.log("BuyButton runs")
return <button onClick={props.onBuy}>Buy 1</button>
}
function App() {
const [stock, setStock] = createSignal(4)
return (
<div class="card">
<h2>The Rainbow Troops</h2>
<p>Stock: {stock()}</p>
<Switch>
<Match when={stock() === 0}><span>Sold out</span></Match>
<Match when={stock() < 3}><span>Running low</span></Match>
<Match when={stock() >= 3}><span>Available</span></Match>
</Switch>
<Show when={stock() > 0} fallback={<p>Out of stock</p>}>
<BuyButton onBuy={() => setStock(s => s - 1)} />
</Show>
<button onClick={() => setStock(s => s + 3)}>Restock +3</button>
</div>
)
}
| Event | stock() |
Label | The Show part |
Console |
|---|---|---|---|---|
| Page opens | 4 | Available |
the Buy 1 button |
BuyButton runs |
Click Buy 1 |
3 | Available |
the same button | |
Click Buy 1 |
2 | Running low |
the same button | |
Click Buy 1 twice |
0 | Sold out |
<p>Out of stock</p> |
|
Click Restock +3 |
3 | Available |
a new button | BuyButton runs |
Showonly swaps its content when its condition turns from true to false or back. Stock going from 4 to 3 is still true, so the button isn't made again.- Its content is made only when needed. At stock 0,
BuyButtonis thrown away. On restock it's made anew, soBuyButton runsprints again.
Switch and Match: the first branch that is true
<Switch> checks the <Match> elements inside it from top to bottom and shows one only: the first whose when is true. The rest aren't rendered at all. If none is true, <Switch fallback={...}> shows.
Before reading on, take a guess: the stock label is written with <Match when={stock() < 3}>Running low</Match> first, then <Match when={stock() === 0}>Sold out</Match>. When stock is 0, is the label Sold out or Running low?
This is where the order matters. If stock() < 3 sits above stock() === 0, at stock 0 both conditions are true (0 is less than 3 too), and the upper one is used: the label is Running low, not Sold out. Put the most specific condition at the top.
The second exercise uses the same shape for a user's role:
import { createSignal, Switch, Match } from "solid-js"
function App() {
const [role] = createSignal("user")
return (
<Switch>
<Match when={role() === "admin"}>
<p>Admin Panel</p>
</Match>
<Match when={role() === "user"}>
<p>Dashboard</p>
</Match>
</Switch>
)
}
The role is "user", so only <p>Dashboard</p> shows. The Admin Panel paragraph is still written in the code, but it isn't on the page.
A ternary works too, so why Show?
In Solid, {stock() > 0 ? <BuyButton /> : <p>Out of stock</p>} also works correctly. Solid's compiler wraps the condition itself, so the branch is also only made again when the condition flips, not on every stock change. What makes Show nicer: a clear name, a fallback, and a function-child form (the next section). The first exercise checks that your code uses Show, so write <Show> there, not a ternary.
💡 Show with a function child, and keyed
<Show when={selected()} fallback={<p>No book selected yet</p>}>
{(book) => <p>{book().title}</p>}
</Show>
When the content of Show is a function, Solid calls it with the value of when, which is then sure to be truthy. In Solid 1.9, book there is a getter, so it's read as book().title. When selected() changes from The Rainbow Troops to This Earth of Mankind, the paragraph stays the same element and only its text is replaced.
With <Show when={selected()} keyed>, the function receives the object itself (book.title), and the whole content is made again every time selected() becomes a different object.
⚠️ The wrong branch shows, or the right one never does
whenwithout parentheses.when={loggedIn}passes the function, and a function is always truthy. In Solid,Welcome!then always shows, whatever the value. Writewhen={loggedIn()}.Matchin the wrong order. A more general condition on top swallows the specific one:stock() < 3abovestock() === 0never letsSold outshow.- Two separate
Showfor one choice. For roles,Switchmakes sure only one paragraph is rendered. The second exercise checks that the page holds a single<p>. - Text with no element. Each branch's content is written as
<p>Dashboard</p>, not bare textDashboard, because what's checked is the content of a<p>. - Forgetting the import.
Show,Switch, andMatchare imported from"solid-js". Without it, a real Solid project stops withReferenceError: Show is not defined.
TL;DR: <Show when={condition()} fallback={<p>...</p>}>content</Show> shows its content while the condition is truthy and the fallback while it's falsy, and only swaps them when the condition flips, so stock going from 4 to 3 doesn't make the Buy button again. <Switch> with several <Match when={...}> shows one branch only: the first true one, so put the most specific condition on top. A ternary works in Solid too, but Show is clearer and has a fallback. A Show function child receives a getter (book()), or the value itself with keyed. Always call the signal in when: when={loggedIn()}.
⚡ Show: when and fallback
Write:
<Show when={loggedIn()} fallback={<p>Please log in</p>}>
<p>Welcome!</p>
</Show>
Meaning: What sits between <Show> and </Show> shows while when is truthy, and the fallback shows while it's falsy. Call the signal with parentheses: when={loggedIn} passes the function, and a function always counts as true.
⚡ Show only swaps when its condition flips
Meaning: Stock going from 4 to 3 is still true, so the Buy button isn't made again. Its content is thrown away only at stock 0, and made anew on restock. A ternary works in Solid too, but Show is clearer and has a fallback.
⚡ Switch and Match: the first true branch
Write:
<Switch>
<Match when={stock() === 0}><span>Sold out</span></Match>
<Match when={stock() < 3}><span>Running low</span></Match>
<Match when={stock() >= 3}><span>Available</span></Match>
</Switch>
Guess first: if stock() < 3 sits above stock() === 0, which label shows at stock 0?
Running low. At stock 0 both conditions are true, and the upper one is used.
Meaning: Switch checks the Match elements from top to bottom and shows only one. Put the most specific condition at the top.