Skip to main content

Reference

shallow

A utility function that performs shallow equality comparison between two values. Used as the default equality function in useStore to prevent unnecessary component re-renders.
T
The first value to compare.
T
The second value to compare.

Returns

boolean
true if the values are shallowly equal, false otherwise.

Comparison Behavior

The shallow function handles various data types:
  • Reference equality: Returns true if Object.is(objA, objB)
  • Primitives/null: Returns false if either value is not an object
  • Map: Compares size and all entries using Object.is
  • Set: Compares size and all values for presence
  • Date: Compares timestamps using getTime()
  • Objects: Compares own properties (including symbols) using Object.is

Usage

Default Behavior

Explicit Usage

Selecting Object Slices

Reference Equality Alternative

Array Comparisons

Map and Set Support

Deep Equality Alternative

Performance Optimization

Notes

  • Used as the default equality function in useStore
  • Only compares own properties at the first level (shallow)
  • For nested objects, only the reference is compared
  • Handles special object types: Map, Set, Date
  • Includes symbol properties in comparison (unlike basic React shallow)
  • More efficient than deep equality for most use cases
  • Prevents unnecessary re-renders when object contents are equivalent
  • Works with Preact’s virtual DOM diffing for optimal performance