AtomOptions
TheAtomOptions<T> type defines configuration options that can be passed when creating an atom to customize its behavior.
Type Definition
Properties
compare
A custom comparison function to determine if the atom’s value has changed. This function is called when setting a new value to decide whether subscribers should be notified.
Type:
===) to compare values.
Returns:
trueif the values are considered equal (no change, don’t notify subscribers)falseif the values are different (notify subscribers)
Usage Examples
Default comparison (reference equality)
Custom deep equality comparison
Shallow comparison for objects
Numeric tolerance comparison
Custom comparison for arrays
Always notify (no comparison)
Conditional comparison logic
Performance optimization with compare
When to Use Custom Comparison
Use custom comparison when:- Object values: You store objects and want to compare by content, not reference
- Performance: You want to avoid unnecessary re-renders or updates
- Tolerance: You need approximate equality (e.g., floating-point numbers)
- Partial comparison: Only certain fields matter for determining changes
- Always notify: You want every
setcall to notify subscribers regardless of value
Best Practices
- Keep it fast: Comparison functions are called on every
set, so keep them efficient - Be consistent: Ensure your comparison logic is symmetric and transitive
- Consider immutability: If you use immutable updates, default reference equality might be sufficient
- Document behavior: If using custom comparison, document why and how it works
Related Types
- Atom - The writable atom type that accepts options
- ReadonlyAtom - Read-only atoms also support comparison options