createComputed
Edit this pagecreateComputed has been removed in Solid 2.0. Its use cases are covered by the split createEffect, createSignal(fn) for writable derived signals, createStore(fn) for derived stores, or createMemo.
Migration
Deriving state — use createMemo
If you used createComputed to derive a value from other signals, use createMemo instead:
// 1.x — createComputed to derive a valueconst [count, setCount] = createSignal(0)const [doubled, setDoubled] = createSignal(0)createComputed(() => setDoubled(count() * 2))
// 2.0 — createMemoconst [count, setCount] = createSignal(0)const doubled = createMemo(() => count() * 2)Writable derived signals — use createSignal(fn)
If you used createComputed to set a signal from reactive data while also allowing manual writes, use createSignal(fn) to create a writable derived signal:
// 1.x — createComputed with a writable signalconst [count, setCount] = createSignal(0)const [label, setLabel] = createSignal("")createComputed(() => setLabel(`Count: ${count()}`))// could also call setLabel("custom") elsewhere
// 2.0 — writable derived signalconst [count, setCount] = createSignal(0)const [label, setLabel] = createSignal(() => `Count: ${count()}`)// label() returns the derived value, but setLabel("custom") can override itDerived stores — use createStore(fn)
If you used createComputed to synchronize store data, use createStore(fn):
// 1.x — createComputed updating a storeconst [source] = createSignal({ name: "Alice", age: 30 })const [state, setState] = createStore({ name: "", age: 0 })createComputed(() => setState(source()))
// 2.0 — derived storeconst [source] = createSignal({ name: "Alice", age: 30 })const [state, setState] = createStore(() => source())Side effects — use split createEffect
If you used createComputed to run side effects on reactive changes, use the split createEffect:
// 1.x — createComputed for side effectscreateComputed(() => { console.log("value changed:", someSignal())})
// 2.0 — split createEffectcreateEffect( () => someSignal(), // compute: track dependencies (value) => console.log("value changed:", value) // apply: side effect)1.x API reference (removed)
import { createComputed } from "solid-js"
function createComputed<T>(fn: (v: T) => T, value?: T): voidcreateComputed created a new computation that immediately ran the given function in a tracking scope, automatically tracking its dependencies and rerunning whenever they changed. The function received the value returned from its last execution, or the optional second argument on the first call.