Skip to content

Show

Show conditionally renders children based on a reactive condition.

Props

PropTypeDescription
whenT | () => TCondition — can be a static value or an accessor
fallbackJSX.ElementOptional element to render when condition is falsy
childrenJSX.ElementContent to render when condition is truthy

Example

tsx
import { sig, val, set, mount, Show } from "@hedystia/view";

function App() {
  const loggedIn = sig(false);

  return (
    <div>
      <button onClick={() => set(loggedIn, !val(loggedIn))}>
        {() => val(loggedIn) ? "Log out" : "Log in"}
      </button>

      <Show
        when={() => val(loggedIn)}
        fallback={<p>Please log in to continue.</p>}
      >
        <div>
          <h2>Welcome back!</h2>
          <p>You are logged in.</p>
        </div>
      </Show>
    </div>
  );
}

mount(App, document.getElementById("root")!);

When loggedIn becomes truthy, the children are inserted into the DOM. When it becomes falsy, they are removed and the fallback is shown instead.