Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 39 ยท Components and Props

Components and Props in SolidJS

Learn how to write SolidJS components: functions that run once, props as reactive getters, passing functions and signals, default values and typing.

A Solid component is a plain function that takes one argument, props, and returns something Solid can render. Because it runs only once, a component works more like a constructor that wires up reactive pieces than like a render function. This lesson covers writing components, passing props, and the one rule that makes props work: do not read them early.

A first component

JSX
function Greeting(props) {
  return <h2>Hello, {props.name}!</h2>;
}

<Greeting name="Ada" />
  • The name starts with a capital letter, so JSX treats it as a component and not an HTML element.
  • props is a single object holding every attribute and the children.
  • Return anything renderable: elements, strings, numbers, arrays, null (renders nothing), or another component.
A reusable Card component

Props are reactive getters

When a parent passes an expression that depends on a signal, Solid does not copy its value. It defines a getter on the props object, so reading props.title runs the expression right then.

JSX
<Card title={name()} />
// roughly: Card({ get title() { return name(); } })

This is how props stay live without a re-render: wherever you read props.title inside a reactive spot (JSX, an effect, a memo), that spot subscribes to the source signal.

That also explains the golden rule.

Do not destructure props

JSX
function Bad({ title }) {          // reads props.title once, now
  return <h3>{title}</h3>;          // never updates
}

function Good(props) {
  return <h3>{props.title}</h3>;    // reads in a reactive spot
}

Destructuring reads each prop once, at setup, and the link to the parent's signal is lost. The same happens with const t = props.title. If you need a derived value, wrap it in a function: const upper = () => props.title.toUpperCase();. Splitting and defaulting props safely is the subject of Props and Reactivity.

Destructured props lose reactivity

(In real JSX the prop would be written value={n()}; the html helper receives the accessor n and wraps it in a getter.)

Passing different kinds of props

You pass Example Notes
A static value <Tag label="new" /> Plain string, number or boolean.
A signal's value <Tag count={n()} /> Becomes a getter; stays reactive.
A function <Button onClick={save} /> Event callbacks are ordinary props.
An accessor <Chart data={series} /> Pass the function if the child should call it itself.
Children <Panel><p>Hi</p></Panel> Available as props.children.
A spread <Input {...rest} /> Forward many props at once.

Pass n() (the value expression) when the child only needs to read it. Pass n (the accessor) when the child needs to call it in several places or wants to pass it further down without evaluating. Both stay reactive.

Default values and optional props

Read a missing prop and you get undefined. For defaults use mergeProps:

JSX
function Button(props) {
  const merged = mergeProps({ type: "button", variant: "primary" }, props);
  return <button type={merged.type} class={merged.variant}>{merged.children}</button>;
}

mergeProps keeps getters reactive, which { ...defaults, ...props } would not. Alternatively, use an inline fallback at the read site: props.variant ?? "primary".

Components that return signals' worth of DOM

Components can hold local state and expose it only through the DOM or through callback props. A typical pattern is a controlled callback prop:

JSX
function Counter(props) {
  const [n, setN] = createSignal(props.start ?? 0);
  return (
    <button onClick={() => { setN(n() + 1); props.onChange?.(n()); }}>
      {n()}
    </button>
  );
}

Notice that props.start is read once on purpose, as an initial value. Tools like the Solid ESLint rules may warn; use untrack(() => props.start) to show intent (see Batching, untrack and on).

Typing props in TypeScript

TSX
import type { Component, ParentComponent } from "solid-js";

const Greeting: Component<{ name: string }> = (props) => <h2>{props.name}</h2>;
const Panel: ParentComponent<{ title: string }> = (props) => (
  <section><h3>{props.title}</h3>{props.children}</section>
);

Component<P> types a component without children, and ParentComponent<P> adds children?: JSX.Element. VoidComponent forbids children. See the TypeScript lessons for more on typing.

Common mistakes

  • Destructuring props in the parameter list or the first line.
  • Reading a prop in the body and expecting updates. Wrap the read in a function or JSX.
  • Mutating props. Props are read-only; send changes up with callbacks.
  • Using {...defaults, ...props}, which flattens getters. Use mergeProps.
  • Lowercase component names. <card /> renders an element named card.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many times does a component function run when a prop's source signal changes?

    Components run once. Props are getters, so reactive spots that read them update.
  2. 2. Why does function Bad({ title }) break reactivity?

    The getter is evaluated once, so the later changes are never seen.
  3. 3. Which helper adds default values while keeping props reactive?

    mergeProps combines objects without evaluating their getters.
  4. 4. How does a child tell the parent something happened?

    Props are read-only; communicate upward through functions passed as props.

Summary

  • A component is a function of props that runs once and returns renderable output.
  • Props are reactive getters: read them inside JSX, effects and memos, not at setup.
  • Never destructure props; use mergeProps for defaults and callbacks for output.
  • Type components with Component, ParentComponent and VoidComponent.

Next: Props and Reactivity