Перейти к содержанию



function createComputed<T>(fn: (v: T) => T, value?: T): void;

createComputed creates a new computation that immediately runs the given function in a tracking scope, thus automatically tracking its dependencies, and automatically reruns the function whenever the dependencies changes. The function gets called with an argument equal to the value returned from the function's last execution, or on the first call, equal to the optional second argument to createComputed. Note that the return value of the function is not otherwise exposed; in particular, createComputed has no return value.

createComputed is the most immediate form of reactivity in Solid, and is most useful for building other reactive primitives. (For example, some other Solid primitives are built from createComputed.) But it should be used with care, as createComputed can easily cause more unnecessary updates than other reactive primitives. Before using it, consider the closely related primitives createMemo and createRenderEffect.

Like createMemo, createComputed calls its function immediately on updates (unless you're in a batch, effect, or transition). However, while createMemo functions should be pure (not set any signals), createComputed functions can set signals. Related, createMemo offers a readonly signal for the return value of the function, whereas to do the same with createComputed you would need to set a signal within the function. If it is possible to use pure functions and createMemo, this is likely more efficient, as Solid optimizes the execution order of memo updates, whereas updating a signal within createComputed will immediately trigger reactive updates some of which may turn out to be unnecessary.


Name Type Description
fn (v: T) => T The function to run in a tracking scope.
value T The initial value to pass to the function.

{/* TODO: Add code playground example */}