Skip to main content

Overview

The withMemo extension prevents unnecessary state updates by comparing the new state with the previous state using an equality function. If the states are equal, the previous reference is kept, preventing downstream computations and subscriptions from re-running. This is particularly useful for:
  • Preventing re-renders when objects have the same values but different references
  • Optimizing performance in complex computed chains
  • Controlling update propagation based on semantic equality

Type Signature

Parameters

function
Function to determine if two states are equal. Defaults to shallow equality check.Parameters:
  • prevState: The previous state
  • nextState: The new state
Returns: true if states are considered equal, false otherwiseDefault: isShallowEqual - Compares object properties and array elements by reference

Return Value

Returns an extension that memoizes the atom’s state.

Examples

Shallow Memoization (Default)

Prevent Array Recreations

Deep Equality

Prevent Computed Recalculation

Prevent Subscription Updates

Custom Equality Function

Numeric Tolerance

Use Cases

Optimize Form State

Prevent List Re-renders

Filter Updates by Significance

Cached API Responses

Stable References for React

Normalized Data

Performance Considerations

Shallow vs Deep Equality

When NOT to Use

Equality Functions

Built-in Options

Custom Implementations