Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 ยท Signals and Reactivity

Ownership, Roots and Cleanup

Understand the SolidJS reactive owner tree: how effects are disposed, what createRoot does, why onCleanup works and how to use runWithOwner safely.

Every effect and memo you create is kept alive by something and removed by something. That "something" is the owner. Understanding ownership explains why Solid never leaks subscriptions in normal code, why you sometimes see the warning "computations created outside a createRoot", and how onCleanup really works.

The owner tree

When Solid runs a component, an effect or a memo, it marks that function as the current owner. Any reactive computation created while it runs becomes a child of that owner. The result is a tree that mirrors your UI:

flowchart TD
    R["render root"] --> A["App component"]
    A --> E1["effect in App"]
    A --> L["List component"]
    L --> M["memo in List"]
    L --> I1["Item 1"]
    L --> I2["Item 2"]
    I1 --> E2["effect in Item 1"]

Disposing a node disposes everything beneath it.

When a parent re-runs or is removed, Solid disposes all its children: their effects stop, their memos are released and their onCleanup callbacks run. That is why an Item component that disappears from a list stops all its timers without you writing teardown code, provided you registered them with onCleanup.

onCleanup attaches to the owner

onCleanup(fn) registers fn on the current owner. It runs when that owner is disposed or, for an effect, before it re-runs.

Cleanup when a component is removed

Toggle the clock off and the console shows "Clock cleaned up": the owner (the Clock component) was disposed, so its cleanup ran. Toggle on again and a brand new Clock is created.

createRoot: an owner of your own

Code that runs outside any component has no owner. If you create an effect there, Solid cannot know when to dispose it and prints a warning. createRoot creates an independent owner and gives you a dispose function so you decide when it ends.

JavaScript
const dispose = createRoot((dispose) => {
  const [n, setN] = createSignal(0);
  createEffect(() => console.log("n =", n()));
  return dispose;
});

// later
dispose();   // the effect stops

Typical uses are module-level state, test code, tools that start Solid in a non-component context, and long-lived global stores. render() itself is built on createRoot, which is why the top of every app is a root.

A root you dispose yourself

After you press dispose the root, the signal keeps working (the button still shows the number) but the effect is gone. Signals themselves are plain values; only computations have owners.

Why async code loses its owner

The owner is set synchronously while a function runs. After an await or inside a setTimeout, there is no current owner anymore, so a createEffect or onCleanup called there is orphaned.

JavaScript
function Bad() {
  setTimeout(() => {
    createEffect(() => console.log(count()));  // no owner: warns, never disposed
  }, 1000);
}

Two ways to fix it:

  1. Create the reactive thing first, outside the callback, and only start the async work inside it.
  2. Capture the owner with getOwner() and re-enter it with runWithOwner.
JavaScript
function Good() {
  const owner = getOwner();
  setTimeout(() => {
    runWithOwner(owner, () => {
      createEffect(() => console.log(count()));   // owned by the component again
    });
  }, 1000);
}

runWithOwner is an advanced tool, and library code uses it more than app code. Reach for it only when you really must create reactive code after an asynchronous gap.

Effects, memos and cleanup order

  • An effect's cleanups run before each re-run and when it is disposed, in reverse order of registration.
  • A child's cleanups run before its parent's.
  • A disposed computation never runs again, even if a signal it read changes.

This ordering is what makes patterns such as "subscribe, then unsubscribe" safe: the old subscription is always gone before the new one starts.

Common mistakes

  • Creating effects at module level without createRoot. You get a warning, and the effect lives forever.
  • Calling onCleanup outside an owner. It has nothing to attach to and logs a warning.
  • Assuming signals are owned. They are not; only effects, memos, components and roots are.
  • Forgetting to dispose manual roots. Roots you create are your responsibility.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Solid do with child computations when an owner is disposed?

    Disposal cascades down the owner tree.
  2. 2. When should you use createRoot?

    createRoot gives you an independent owner and a dispose function.
  3. 3. Why can a createEffect inside a setTimeout callback be orphaned?

    After the synchronous run ends there is no current owner, unless you restore it with runWithOwner.
  4. 4. Which function re-enters a captured owner?

    getOwner captures it; runWithOwner(owner, fn) runs fn under that owner.

Summary

  • Computations form an owner tree; disposing a node disposes its children.
  • onCleanup registers a callback on the current owner.
  • createRoot makes an independent owner you dispose yourself.
  • Ownership is tracked synchronously; use getOwner and runWithOwner after async gaps.
  • Signals are not owned, computations are.

Next: Component Lifecycle