Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 39 ยท JSX and Control Flow

Conditional Rendering: Show, Switch and Match

Render conditionally in SolidJS with Show, fallback, keyed and callback children, and Switch and Match for multiple branches, with live examples.

Because a Solid component runs only once, an if statement in the component body decides once and never again. Conditional display has to be reactive, which is what Show, Switch and Match are for. They create and destroy DOM branches precisely when the condition changes.

The problem with plain if

JSX
function Bad() {
  const [loggedIn, setLoggedIn] = createSignal(false);
  if (loggedIn()) {            // runs once, at creation: never updates
    return <Dashboard />;
  }
  return <Login />;
}

The if reads the signal during setup, so the result is frozen. Put the condition inside the returned JSX in a reactive spot, and use Show.

Show

JSX
<Show when={loggedIn()} fallback={<Login />}>
  <Dashboard />
</Show>
  • when is the condition. Any truthy value shows the children.
  • fallback is optional and displays when when is falsy.
  • The children are created when the condition becomes true and disposed when it becomes false, so their effects and cleanups run properly. Switching back creates a new copy.
Show with a fallback

Open and close the panel and read the console: it really is created and disposed each time.

Using the value: keyed and callback children

Show can pass the truthy value to its children, which is useful for nullable data. Give it a function as the child:

JSX
<Show when={user()} fallback={<p>Signed out</p>}>
  {(u) => <p>Hello, {u().name}</p>}
</Show>

Here u is an accessor (a getter function) that is guaranteed non-null, and it updates when the user object is replaced. In older code you may see keyed, which passes the plain value and re-creates the children whenever the object identity changes:

JSX
<Show when={user()} keyed>
  {(u) => <p>Hello, {u.name}</p>}
</Show>

Prefer the accessor form: it keeps the DOM when only fields change.

Switch and Match

For more than two branches, Switch shows the first Match whose when is truthy, like if / else if / else.

JSX
<Switch fallback={<p>Idle</p>}>
  <Match when={status() === "loading"}><Spinner /></Match>
  <Match when={status() === "error"}><ErrorMessage /></Match>
  <Match when={status() === "ready"}><Results /></Match>
</Switch>

Only one branch exists in the DOM at a time. The fallback of Switch is used when no Match is true.

Switch and Match for a status machine

Nested and combined conditions

  • Compute a condition once with a function or memo and reuse it: const isEmpty = () => items().length === 0.
  • Nest Show inside Show for dependent checks, or use a Switch with ordered Match branches (more readable).
  • Like Match, when may hold any value; Match also supports the callback child form.
JSX
<Switch>
  <Match when={error()}>{(e) => <p>Error: {e().message}</p>}</Match>
  <Match when={data()}>{(d) => <List items={d()} />}</Match>
</Switch>

Why not the ternary?

{cond() ? <A /> : <B />} works, since the whole expression is reactive. The difference is how it updates: Solid re-evaluates the expression when cond() changes, which is fine, but any signal read inside that expression also re-runs it and may recreate DOM unnecessarily. Show memoizes the condition, so the branch is only rebuilt when the truthiness changes. Use ternaries for small text choices, and Show / Switch for components.

Common mistakes

  • Using if in the component body and expecting updates.
  • cond && <X /> with a number, such as {count() && <X />}, which renders 0 when count is zero. Use Show or count() > 0.
  • Forgetting that the branch is destroyed. Local state inside the children resets when the branch is hidden. Keep state above Show if it must survive.
  • Passing a value instead of a function to the callback form: the child must be a function.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does an if statement in a Solid component body not react to signal changes?

    The body runs once, so a condition read there is evaluated once. Put conditions inside reactive JSX.
  2. 2. What happens to Show's children when when becomes falsy?

    The branch is torn down, running its cleanups; showing it again builds a new copy.
  3. 3. In Switch, how many Match branches render?

    Switch behaves like if / else if: the first matching branch wins.
  4. 4. What does the callback child of Show receive in the non-keyed form?

    You call it, as in u(), and it stays up to date when the object changes.

Summary

  • Use Show for one condition with an optional fallback; branches are created and disposed on demand.
  • A function child gives you a non-null accessor to the value.
  • Switch / Match handle several exclusive branches.
  • Avoid if in the component body and number-valued && expressions.

Next: For, Index and Lists