memo (@shadow-js/web/server)
The server twin of memo: nothing is reactive during a server render, so it computes once, on the first read, and returns the cached value on every later read.
The server twin of memo: nothing is reactive during a server render, so it computes once, on the
first read, and returns the cached value on every later read. A call that throws is not cached
(the next read runs fn again).
Import#
ts
import { memo } from "@shadow-js/web/server";
Signature#
ts
function memo<T>(fn: () => T): () => T
Parameters#
| Name | Description |
|---|---|
fn |
The computation; runs untracked, on the first read. |
Returns#
An accessor of the cached value.
Throws#
- TypeError from the accessor when
fnreads the accessor itself while it runs, instead of recursing. An errorfnthrows propagates from the read.
Examples#
ts
import { memo } from "@shadow-js/web/server";
const items = [3, 1, 2];
const sorted = memo(() => [...items].sort());
sorted(); // [1, 2, 3], computed now
sorted(); // the same array, cached