Ripple-Reactive
API Reference

computed()

Creates a lazy, derived reactive value.

Creates a derived signal that evaluates a compute function and tracks any signals read during its execution. The evaluation is lazy (it does not run until read) and memoized (it only re-runs if its dependencies have changed).

function computed<T>(
  computeFn: () => T, 
  options?: ComputedOptions<T>
): ComputedSignal<T>;

Parameters

ParameterTypeDescription
computeFn() => TA pure, synchronous function that returns the derived value.
optionsComputedOptions<T>(Optional) Configuration object to customize behavior, such as custom equality checks.

ComputedOptions

PropertyTypeDescription
equals(prev: T, next: T) => booleanOverrides the default strict equality (===) check to determine if the new value should trigger downstream effects.

Returns

Returns a ComputedSignal<T> object (which is strictly read-only) with the following interface:

PropertyTypeDescription
valueTA getter property for the current derived value. Attempting to set this property will throw an error.
peek()() => TReads the current value without creating a reactive subscription to this computed signal.

Usage

Basic Derivation

Computed functions automatically subscribe to any signals they read. If count changes, double is marked as dirty but won't re-calculate until .value is accessed.

import { signal, computed } from 'ripple-reactive';

const count = signal(2);
const double = computed(() => count.value * 2);

console.log(double.value); // 4

Custom Equality

By default, Ripple.js avoids triggering downstream effects if a computed value evaluates to the exact same result (===). For objects or arrays, you can provide a custom equals function to prevent unnecessary updates when the internal data hasn't logically changed.

import { signal, computed } from 'ripple-reactive';

const userFirst = signal('Aditya');
const userLast = signal('Singh');

const userProfile = computed(
  () => ({ 
    firstName: userFirst.value, 
    lastName: userLast.value 
  }),
  {
    // Deep comparison to prevent re-renders if the resulting object is identical
    equals: (prev, next) => 
      prev.firstName === next.firstName && 
      prev.lastName === next.lastName
  }
);

Compute functions must be pure. Do not mutate other signals or trigger side-effects (like DOM updates or API calls) inside a computed() callback. Use effect() for side-effects.

On this page