shallowObjectEqualityCheck
Shallow equality check for objects with primitive property values. Compares each property's value rather than object reference.
When to Use
Use when deriving objects where all properties are primitives (string, number, boolean):
- ✅ Objects with primitive values only
- ✅ Combining multiple facets into a single object
- ❌ Objects with nested objects (use
createUniformObjectEqualityCheck) - ❌ Objects with arrays (use
createObjectWithKeySpecificEqualityCheck)
Signature
typescriptfunction shallowObjectEqualityCheck(): (current: ObjectWithImmutables) => booleantype ObjectWithImmutables = Record<string, string | number | boolean>
Basic Usage
tsximport {shallowObjectEqualityCheck } from '@react-facet/core'constequalityCheck =shallowObjectEqualityCheck ()equalityCheck ({name : 'Alex',height : 2 })console .log (equalityCheck ({name : 'Alex',height : 2 })) // true - same valuesconsole .log (equalityCheck ({name : 'Steve',height : 2 })) // false - different nameconsole .log (equalityCheck ({name : 'Steve',height : 2 })) // true - same values again
Usage with Facets
Combining Multiple Facets
tsximport {useFacetMap ,useFacetWrap ,shallowObjectEqualityCheck } from '@react-facet/core'constPlayerStats = () => {constnameFacet =useFacetWrap ('Steve')consthealthFacet =useFacetWrap (100)constlevelFacet =useFacetWrap (5)// Combine into single object - only updates when values actually changeconstplayerStatsFacet =useFacetMap ((name ,health ,level ) => ({name ,health ,level }),[],[nameFacet ,healthFacet ,levelFacet ],shallowObjectEqualityCheck , // Prevents updates from object recreation)return (<div ><fast-text text ={useFacetMap ((stats ) => `${stats .name } - Level ${stats .level }`, [], [playerStatsFacet ])} /></div >)}
Nullable Variant
For cases where the entire object might be null or undefined, use nullableShallowObjectEqualityCheck:
tsximport {nullableShallowObjectEqualityCheck } from '@react-facet/core'constequalityCheck =nullableShallowObjectEqualityCheck ()equalityCheck (null)console .log (equalityCheck (null)) // true - null === null ✅console .log (equalityCheck ({name : 'Steve',age : 25 })) // false - null → object ❌console .log (equalityCheck ({name : 'Steve',age : 25 })) // true - same object values ✅console .log (equalityCheck (null)) // false - object → null ❌
For objects with nullable properties (like { name: string | null }), you don't need the nullable variant. Regular shallowObjectEqualityCheck handles null/undefined property values just fine using strict equality (===).
How It Works
The equality check:
- Compares the number of keys in both objects
- Iterates through each property
- Compares primitive values using
=== - Returns
falseif any property differs
tsximport {shallowObjectEqualityCheck } from '@react-facet/core'constcheck =shallowObjectEqualityCheck ()check ({a : 1,b : 'hello' })// Different object reference, but same valuesconsole .log (check ({a : 1,b : 'hello' })) // true ✅// Different valuesconsole .log (check ({a : 2,b : 'hello' })) // false ❌// Different keysconsole .log (check ({a : 1,b : 'hello',c : 'extra' })) // false ❌
Important Notes
Only Shallow Comparison
This check does not compare nested objects or arrays:
tsximport {shallowObjectEqualityCheck } from '@react-facet/core'// ❌ Won't work as expected for nested structuresconstcheck =shallowObjectEqualityCheck ()constnested1 = {user : {name : 'Steve' } }constnested2 = {user : {name : 'Steve' } } // Different object reference// @ts-expect-error - Type error: user property is an object, not a primitivecheck (nested1 )// @ts-expect-error - Type error: user property is an object, not a primitiveconsole .log (check (nested2 )) // false - user objects have different references
Note: TypeScript will catch this mistake - the equality check expects objects with only primitive values (string | number | boolean).
For nested structures, use:
createUniformObjectEqualityCheckfor uniform nested valuescreateObjectWithKeySpecificEqualityCheckfor mixed nested values
Property Order Doesn't Matter
tsximport {shallowObjectEqualityCheck } from '@react-facet/core'constcheck =shallowObjectEqualityCheck ()check ({name : 'Alex',age : 25 })// Same values, different order - still equalconsole .log (check ({age : 25,name : 'Alex' })) // true ✅
Common Patterns
Form State
tsximport { useFacetMap, shallowObjectEqualityCheck } from '@react-facet/core'const formStateFacet = useFacetMap((username, email, age) => ({ username, email, age }),[],[usernameFacet, emailFacet, ageFacet],shallowObjectEqualityCheck,)
Computed Properties
tsximport { useFacetMap, shallowObjectEqualityCheck } from '@react-facet/core'const statsViewFacet = useFacetMap((player) => ({displayName: player.name.toUpperCase(),healthPercent: (player.health / player.maxHealth) * 100,isLowHealth: player.health < 20,}),[],[playerFacet],shallowObjectEqualityCheck,)
API Response Transformation
tsximport { useFacetMap, shallowObjectEqualityCheck } from '@react-facet/core'const parsedResponseFacet = useFacetMap((rawData) => ({id: parseInt(rawData.id),name: rawData.name.trim(),active: rawData.status === 'active',}),[],[rawDataFacet],shallowObjectEqualityCheck,)
See Also
- Equality Checks Overview - Guide to all equality checks
strictEqualityCheck- For primitive valuescreateUniformObjectEqualityCheck- For objects with nested structurescreateObjectWithKeySpecificEqualityCheck- For objects with mixed property types