DocsLearn

Showing and hiding

A condition in JSX is plain JavaScript: show a part when something holds, another when it doesn’t, or one of several.

In this lesson

  • how to show something only when a condition holds;
  • how to show one thing or another;
  • how to choose one of several;
  • what happens to what was typed in a part that is hidden.

Only when it holds

{condition && <Part />} shows the part while the condition holds, and nothing otherwise.

import { useState } from "react";
import { Button } from "@/components/ui/button";

export default function App() {
  const [open, setOpen] = useState(false);
  return (
    <div className="grid gap-3">
      <Button variant="secondary" onClick={() => setOpen((o) => !o)}>{open ? "Hide" : "Show"} the details</Button>
      {open && <p>Ships in two days, free over €50.</p>}
    </div>
  );
}

Mind a number on the left: {count && <p>…</p>} shows 0 when count is 0. Write {count > 0 && …}.

One or the other

A ternary chooses between two:

import { useState } from "react";
import { Button } from "@/components/ui/button";

export default function App() {
  const [signedIn, setSignedIn] = useState(false);
  return (
    <div className="flex items-center gap-3">
      {signedIn ? <p>Welcome back, Ada.</p> : <p>You are not signed in.</p>}
      <Button variant="outline" onClick={() => setSignedIn((s) => !s)}>{signedIn ? "Sign out" : "Sign in"}</Button>
    </div>
  );
}

One of several

For more than two, look the part up by name, with a default when there is none:

import { useState } from "react";
import { Button } from "@/components/ui/button";

const PARTS = {
  posts: <p>The latest posts.</p>,
  people: <p>Everyone on the team.</p>,
};

export default function App() {
  const [tab, setTab] = useState("posts");
  return (
    <div className="grid gap-3">
      <div className="flex gap-1">
        {["posts", "people", "settings"].map((name) => (
          <Button key={name} variant={tab === name ? "secondary" : "ghost"} onClick={() => setTab(name)}>{name}</Button>
        ))}
      </div>
      {PARTS[tab] ?? <p>Nothing here yet.</p>}
    </div>
  );
}

Inside a component of its own, an early return reads well too: if (!user) return <SignIn />;.

What a hidden part forgets

A part taken away by a condition is gone with its state: what was typed in it is lost, and it starts afresh when shown again. To keep it, keep the state in the component above (see State), or hide the part without taking it away: <div hidden={!open}>.

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";

export default function App() {
  const [open, setOpen] = useState(true);
  return (
    <div className="grid gap-3">
      <Button variant="secondary" onClick={() => setOpen((o) => !o)}>{open ? "Hide" : "Show"} both</Button>
      {open && <Input placeholder="Taken away: forgets" />}
      <div hidden={!open}><Input placeholder="Hidden: remembers" /></div>
    </div>
  );
}

Type in both, hide them, show them again.

Next: draw a row for each item in a list, with Lists.

View as Markdown