Under The Hood
frontend solidjs SolidJS 1.x / 2.x

<Dynamic component={...}>: Element Switching Without Full Re-Creation

Last updated
Prerequisites:
SolidJS Components Run Once
SolidJS JSX
  • solidjs
  • Dynamic
  • components
  • polymorphism
  • rendering
  • control-flow

Read at your depth

The practical view

`<Dynamic component={componentAccessor()}>` renders a component or HTML tag dynamically based on a reactive signal or prop. `import { Dynamic } from 'solid-js/web';`. You can pass either component functions (`<Dynamic component={MyComponent} />`) or HTML tag string names (`<Dynamic component="h1" />`). When the `component` signal updates, Solid swaps only the dynamic element.

The same idea in other frameworks

react equivalent
`const Tag = tagSignal; return <Tag {...props} />;` inside render.

Legacy vs modern

Nested Ternary Component Switching vs <Dynamic>

Ternary trees for dynamic tags become unreadable; `<Dynamic>` cleanly maps runtime tags and components.

before → after
Clunky Ternary Tree
{tag() === 'h1' ? <h1>{text}</h1> : tag() === 'h2' ? <h2>{text}</h2> : <p>{text}</p>} // ❌ Repetitive
Solid <Dynamic> Component
<Dynamic component={level()} class="title">{children}</Dynamic> // ✅ NEW: Clean & polymorphic

Interview gotchas

Context

Tests knowledge of polymorphic component capabilities.

The mechanical answer

Yes. If `component` is passed a string (e.g. `'div'`, `'button'`, `'svg'`), `<Dynamic>` creates a native DOM element with the provided props. If `component` is passed a component function reference (e.g. `MyCustomButton`), it invokes `createComponent(component, props)`.

Trap

Passing an invalid value like `null` or `undefined` to `component` will result in `<Dynamic>` rendering nothing (an empty text node).

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.