Home > @pago/reactive > derived

derived() function

Returns a ReadonlyRef whose value will always point to the latest result of the given function. The function will only be executed once per set of values.

Signature:

export declare function derived<T>(fn: () => T): ReadonlyRef<T>;

Parameters

Parameter Type Description
fn () => T A function which returns a derivation of tracked objects or references.

Returns:

ReadonlyRef<T>

Example

const name = ref('Preact');
const greet = derived(() => `Hello ${name.current}!`);
console.log(greet.current); // => 'Hello Preact'
name.current = 'React';
console.log(greet.current); // => 'Hello React'