Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 39 ยท JSX and Control Flow

Dynamic, Portal and ErrorBoundary

Use SolidJS control-flow helpers: Dynamic for runtime components, Portal for modals and overlays, ErrorBoundary for failures and the NoHydration idea.

Beyond Show, Switch and For, Solid ships a few more control-flow components for situations that come up in real apps: choosing a component at runtime, rendering outside the parent's DOM position, and containing errors. This lesson covers Dynamic, Portal and ErrorBoundary.

Dynamic: choose the component at runtime

Sometimes the component or tag name depends on data: a rich-text block that is a h1 or a p, or a form field that is a TextInput or a Select. Dynamic renders whatever you give to component.

JSX
import { Dynamic } from "solid-js/web";

const widgets = { text: TextField, choice: SelectField, flag: Checkbox };

<Dynamic component={widgets[field.type]} label={field.label} />

Every extra prop is forwarded to the chosen component. component can also be a string tag name, which creates that element:

JSX
<Dynamic component={level() === 1 ? "h1" : "h2"}>Title</Dynamic>

When component changes, the old one is disposed and the new one is created. When it stays the same, props update reactively as usual.

Dynamic picks the tag at runtime

Portal: render somewhere else

A portal puts DOM nodes in a different place in the document while keeping them logically inside your component. This is how modals, tooltips and toasts avoid being clipped by a parent's overflow: hidden or stacked under other elements.

JSX
import { Portal } from "solid-js/web";

<Show when={open()}>
  <Portal>
    <div class="modal">
      <p>I live under document.body.</p>
      <button onClick={() => setOpen(false)}>Close</button>
    </div>
  </Portal>
</Show>

By default the content goes under document.body. Pass mount to choose another element. The portal's content still belongs to the same owner and has access to the same context, so signals, props and cleanup work as normal. Events bubble through the DOM position of the content, so a click inside a portal does not reach the DOM ancestors where the component was written; share state through signals instead.

A modal rendered through a Portal

ErrorBoundary: contain failures

If code throws while rendering, an unhandled error would break the whole tree. ErrorBoundary catches errors thrown by its children (during rendering, in effects and memos created beneath it) and displays a fallback.

JSX
<ErrorBoundary fallback={(err, reset) => (
  <div>
    <p>Something broke: {err.message}</p>
    <button onClick={reset}>Try again</button>
  </div>
)}>
  <Widget />
</ErrorBoundary>

The fallback may be a node or a function that receives the error and a reset function. Calling reset() re-creates the children, which is perfect after fixing the cause (for example after a retry).

Things an error boundary does not catch: errors inside event handlers (which are ordinary callbacks; use try/catch) and errors in setTimeout or promise callbacks created outside the reactive scope. Put a boundary around each independent region (a widget, a route), so one failure does not blank the whole page.

ErrorBoundary with reset

Other helpers worth knowing

  • Suspense and SuspenseList wait for async data; see Resources, Suspense and Async (chapter 6).
  • NoHydration marks a subtree that the server renders but the client never hydrates.
  • children() resolves a component's children to real nodes, covered in Children and Composition.
  • lazy() loads a component on demand with code splitting.

Common mistakes

  • Putting one boundary at the very top only. A failure then replaces the whole app; use smaller boundaries.
  • Expecting event-handler errors to be caught. Wrap those with try/catch.
  • Forgetting that Dynamic's component can be a string. Passing a tag name is often the simplest case.
  • Using a portal for layout. Portals are for overlays; ordinary content should stay in flow.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the component prop of Dynamic accept?

    Dynamic renders either a component function or an intrinsic element name.
  2. 2. Why use Portal for a modal?

    The DOM nodes mount elsewhere (for example under body) but the component stays in the same reactive owner.
  3. 3. Which error is NOT caught by ErrorBoundary?

    Event handlers are plain callbacks outside the render tree; use try/catch there.
  4. 4. What does calling reset in an ErrorBoundary fallback do?

    reset retries rendering the children, typically after fixing the cause.

Summary

  • Dynamic renders a component or tag chosen at runtime and forwards props.
  • Portal mounts content elsewhere in the DOM, ideal for modals and tooltips.
  • ErrorBoundary shows a fallback for render and effect errors, with a reset function.
  • Use several small boundaries, and try/catch in event handlers.

Next: Events, Styling and classList