Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 39 ยท Components and Props

Props and Reactivity: splitProps and mergeProps

Handle props safely in SolidJS: why destructuring breaks, defaults with mergeProps, splitProps for wrappers, forwarding rest props, and the children helper.

The previous lesson gave you the rule: props are getters, so do not read them early. Real components need more than that: default values, wrappers that forward extra attributes to an inner element, and sometimes a one-time read for an initial value. Solid provides mergeProps and splitProps for these jobs, and this lesson shows where each belongs.

Why props need special tools

Props live on an object whose properties may be getters. Ordinary JavaScript object operations ({...props}, Object.assign, destructuring, const {a, ...rest} = props) evaluate every getter. They hand you a snapshot, and reactivity is gone. mergeProps and splitProps do the same jobs without calling the getters.

mergeProps: defaults and overrides

JSX
import { mergeProps } from "solid-js";

function Badge(props) {
  const p = mergeProps({ color: "gray", size: "md" }, props);
  return <span class={`badge ${p.color} ${p.size}`}>{p.label}</span>;
}
  • Sources are merged left to right, so later sources win. Put defaults first and props last.
  • A prop explicitly passed as undefined overrides the default with undefined in some versions of the merge; use ?? when you want "undefined falls back", e.g. props.color ?? "gray".
  • The result is reactive: if props.color is backed by a signal, p.color follows it.

You can also use mergeProps to combine two reactive objects, such as local overrides plus parent props.

Defaults that stay reactive

The first badge uses both defaults. The second takes label from props and a colour that flips when you press the button, proof that the merged object still tracks the signal.

splitProps: separate what you use from what you forward

Wrapper components often take a few props of their own and pass the rest to an inner element. splitProps returns several reactive objects: one per list of keys you name, plus one with everything else.

JSX
import { splitProps } from "solid-js";

function Button(props) {
  const [local, others] = splitProps(props, ["variant", "children"]);
  return (
    <button class={`btn ${local.variant ?? "plain"}`} {...others}>
      {local.children}
    </button>
  );
}

<Button variant="primary" type="submit" disabled={busy()} onClick={save}>Save</Button>

local holds variant and children. others holds type, disabled, onClick and anything else, still reactive, and is spread onto the real <button>. Compared with React's { variant, ...rest }, the logic is the same; only the mechanism differs.

You can split several groups at once: splitProps(props, ["a"], ["b", "c"]) returns three objects.

A button wrapper that forwards the rest

Turn busy on: the Save button becomes disabled even though Button never mentioned disabled, because it was forwarded reactively with the other props. (The html helper has no compiler, so the demo forwards the keys by hand; in JSX you would write {...others} as shown above.)

Reading a prop once on purpose

Sometimes you want a prop only as an initial value, like the starting value of an input:

JSX
function NameField(props) {
  const [value, setValue] = createSignal(untrack(() => props.initial));
  ...
}

Wrapping the read in untrack says the single read is intended and silences the lint rule solid/reactivity. Document it in the prop name (initialValue, defaultValue) so callers know later changes are ignored.

Derived props

If a child needs a transformation of a prop, define a function or memo, not a snapshot:

JSX
function Price(props) {
  const formatted = () => props.amount.toFixed(2);       // re-evaluates on read
  const label = createMemo(() => `${props.currency} ${formatted()}`);
  return <span>{label()}</span>;
}

Use a plain function for cheap work and a memo for costly or widely read results, as described in Derived Values and createMemo.

children are props too

props.children is a getter that creates the child nodes each time you read it. Reading it twice builds the DOM twice, and reading it conditionally can create and discard nodes. Use it once in JSX, or resolve it with the children helper described in Children and Composition.

Checklist for props

  1. Never destructure props in the signature or body.
  2. Defaults: mergeProps(defaults, props).
  3. Wrappers: splitProps(props, [...ownKeys]) and spread the rest.
  4. Initial values: untrack(() => props.x) with a clear name.
  5. Derived values: functions or memos.
  6. Output: callback props, never assigning to props.

Common mistakes

  • { ...props } for forwarding. It snapshots every getter. Use splitProps.
  • Passing defaults last in mergeProps(props, defaults), so the defaults override real props.
  • Reading props.children twice, creating duplicate DOM.
  • Listing a key in splitProps that you also need to forward. Keys listed are removed from the rest object.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is { ...props } risky in Solid?

    Object spread calls each getter once and stores the plain value.
  2. 2. What does splitProps(props, ["variant"]) return?

    You get [local, others], both still reactive.
  3. 3. In mergeProps(defaults, props), which source wins on a conflict?

    Sources are merged left to right with later ones taking precedence.
  4. 4. How do you read props.initial once, intentionally?

    untrack marks the one-time read as deliberate.

Summary

  • Spread, Object.assign and destructuring evaluate getters and break reactivity.
  • mergeProps adds defaults reactively; later sources win.
  • splitProps separates your props from the ones you forward.
  • Use untrack for deliberate one-time reads, functions or memos for derived values.
  • Read props.children once, or resolve it with the children helper.

Next: Children and Composition