Effects
Edit this pageEffects are functions that are triggered when the signals they depend on change. They play a crucial role in managing side effects, which are actions that occur outside of the application's scope, such as DOM manipulations, data fetching, and subscriptions.
In Solid 2.0, createEffect uses a two-phase model: a compute function that tracks reactive dependencies, and an apply function that performs side effects. This is a fundamental change from Solid 1.x where effects used a single callback. The split design ensures that all dependencies are recorded before any side effects run, which is critical for async, Loading, and Errored boundaries.
Using an effect
An effect is created using the createEffect function.
In Solid 2.0, this function takes two callbacks:
- A compute function — runs in the reactive tracking phase. It should only read reactive values and return a value.
- An apply function — receives the computed value and performs side effects. It can return a cleanup function.
import { createEffect } from "solid-js";
const [count, setCount] = createSignal(0);
createEffect( () => count(), // compute: track dependencies, return value (value) => { // apply: run side effects with the tracked value console.log(value); });In this example, the compute function tracks count and returns its value. When count changes, the apply function runs with the new value and logs it to the console.
Managing dependencies
Dependencies are determined by the compute function. Any signals read inside the compute function are automatically tracked. The apply function does not track dependencies — it is purely for side effects.
The compute function runs whenever any of its tracked dependencies change. The apply function then receives the new computed value.
createEffect( () => [count(), message()], // track multiple dependencies ([countVal, messageVal]) => { console.log(countVal, messageVal); });When either count or message changes, the compute function re-runs, and the apply function receives the updated array.
Cleanup
In Solid 2.0, cleanup is handled by returning a function from the apply callback. This replaces the pattern of calling onCleanup inside effects.
createEffect( () => name(), (value) => { const id = setInterval(() => console.log(value), 1000);
// Return a cleanup function — called before the next apply run or on disposal return () => clearInterval(id); });onCleanup remains available for cleanup inside computations (like createMemo), but is not expected to be used inside effect apply functions. Use the return-based cleanup pattern instead.
Effects should not write signals
Writing to signals inside effects, memos, or component bodies will produce a dev warning in Solid 2.0. Effects are for interacting with the outside world, not synchronizing the reactive system.
// ❌ warns: writing a signal inside an effectcreateEffect( () => count(), (value) => { setDoubled(value * 2); // this will warn });
// ✅ use createMemo for derived valuesconst doubled = createMemo(() => count() * 2);
// ✅ write signals in event handlersbutton.onclick = () => setCount((c) => c + 1);If you truly need to write a signal from within a reactive scope (e.g. internal component flags), create the signal with { pureWrite: true }.
Lifecycle functions
onSettled
onMount is removed in Solid 2.0. Use onSettled instead. Unlike onMount, onSettled can return a cleanup function and runs when the current activity is settled (e.g. after mount, or when the reactive graph is idle).
In situations where you want to run a side effect once after the component is settled, use onSettled:
import { onSettled } from "solid-js";
function Component() { const [data, setData] = createSignal(null);
onSettled(async () => { // runs once when the component is settled const fetchedData = await fetch("https://example.com/data"); setData(fetchedData);
// can return cleanup return () => { // cleanup when component is disposed }; });
return <div>...</div>;}Unlike other tracked scopes, onSettled cannot create nested reactive primitives (such as createSignal or createEffect inside its callback). This is a breaking change from Solid 1.x's onMount.
onCleanup
onCleanup is used for cleaning up a task when it is no longer needed.
onCleanup will run whenever the component unmounts or the enclosing computation re-runs, removing any subscriptions.
import { onCleanup } from "solid-js";
function App() { const [count, setCount] = createSignal(0);
const timer = setInterval(() => { setCount((prev) => prev + 1); }, 1000);
onCleanup(() => { clearInterval(timer); });
return <div>Count: {count()}</div>;}onCleanup is best used inside computations (createMemo, the setup phase of components). For effects, prefer the return-based cleanup from the apply function.
createTrackedEffect
For special cases where you need a single-callback effect (similar to the 1.x pattern), createTrackedEffect is available. However, be aware that it may re-run in async situations and is not the default.
import { createTrackedEffect } from "solid-js";
createTrackedEffect(() => { console.log(count()); // single-callback form — tracks and runs side effects});Prefer the split two-phase createEffect for most use cases.