[]previous and next page

derived

Wraps a computation as a Derived value that recomputes only when the signals it reads change.

Wraps a computation as a Derived value that recomputes only when the signals it reads change. The compiler emits it for const x = <expression reading signals> inside components; it is not meant to be written by hand.

Import#

ts
import { derived } from "@shadow-js/web";

Signature#

ts
function derived<T>(compute: () => T): Derived<T>

Parameters#

Name Description
compute The computation. Runs in a memo, so it is tracked and cached between reads.

Returns#

A Derived whose value (and call result) follows compute.

Examples#

ts
import { useSignal } from "@shadow-js/core";
import { derived } from "@shadow-js/web";

function setup() {
  const [price] = useSignal(20);
  const total = derived(() => price() * 3);
  console.log(total.value); // 60
}