[]previous and next page

createComputation

Creates a memo or effect node: the building block of useMemo, useEffect and the render effects of a renderer.

Creates a memo or effect node: the building block of useMemo, useEffect and the render effects of a renderer. Memos compute immediately unless lazy; render effects run immediately; user effects are queued for the next flush. The node is owned and disposed with its owner. Errors of a memo are stored and rethrown to its readers; errors of an effect go to the owner's error handler.

Import#

ts
import { createComputation } from "@shadow-js/core/extend";

Signature#

ts
function createComputation(options: ComputationOptions): Computation

Parameters#

Name Description
options What to create, how it runs and where it is owned.

Returns#

The node. Read a memo with readComputation; an effect needs no handle.

Throws#

  • The error of a render effect's first run when no error handler takes it and no flush is running.

Examples#

ts
import { createComputation, createSignalNode, readComputation, readSignal, writeSignal } from "@shadow-js/core/extend";

const price = createSignalNode<number>(10, Object.is);
const withTax = createComputation({ kind: "memo", fn: () => readSignal(price) * 1.2 });
createComputation({
  kind: "effect",
  fn: () => console.log(readComputation<number>(withTax)),
});
writeSignal(price, 20); // the effect logs 24 on the next flush