Overview
ThewithMemo 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 statenextState: The new state
true if states are considered equal, false otherwiseDefault: isShallowEqual - Compares object properties and array elements by referenceReturn 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
Related
- withComputed - Add computed behavior
- isShallowEqual - Shallow equality checker
- isDeepEqual - Deep equality checker