useFacetWrapMemo
The useFacetWrapMemo hook is a memoized version of useFacetWrap that maintains a stable facet reference even when the wrapped value changes. Unlike useFacetWrap which creates a new facet when the wrapped value changes, useFacetWrapMemo creates the facet once and updates its value internally.
Signature
typescript
Parameters:
- prop: A- Facet<T>or plain value- T(via- FacetProp<T>)
- equalityCheck: Optional equality check function to prevent unnecessary updates (default:- defaultEqualityCheck)
Returns: A stable Facet<T> that updates its internal value when prop changes
When to Use
Use useFacetWrapMemo instead of useFacetWrap when:
- Facet reference stability matters - When passing the facet to child components that should not re-render when the facet reference changes
- Wrapping frequently changing props - When the prop value changes often but you want to maintain a stable facet reference
Note: useFacetWrapMemo always creates a new facet internally (even when the prop is already a facet) and sets up an observer. This means there is overhead compared to passing facets directly. Use this hook when the stability of the facet reference is more important than avoiding the additional facet creation and subscription.
Comparison with useFacetWrap
useFacetWrap (creates new facet on value change):
tsxuseFacetWrap ,FacetProp } from '@react-facet/core'constComponent = ({value }: {value :FacetProp <string> }) => {// Creates a new facet instance when value changesconstfacet =useFacetWrap (value )return <fast-text text ={facet } />}
useFacetWrapMemo (stable facet, updates internal value):
tsxuseFacetWrapMemo ,FacetProp } from '@react-facet/core'constComponent = ({value }: {value :FacetProp <string> }) => {// Facet instance remains stable, only the value updatesconstfacet =useFacetWrapMemo (value )return <fast-text text ={facet } />}
Basic Usage
Wrapping a prop value that might be a facet or plain value, with stable facet reference:
tsxuseFacetWrapMemo ,useFacetMap ,FacetProp } from '@react-facet/core'typeButtonProps = {label :FacetProp <string>isDisabled :FacetProp <boolean>}constButton = ({label ,isDisabled }:ButtonProps ) => {// Both facets remain stable across re-rendersconstlabelFacet =useFacetWrapMemo (label )constisDisabledFacet =useFacetWrapMemo (isDisabled )constclassName =useFacetMap ((disabled ) => (disabled ? 'button-disabled' : 'button-active'), [], [isDisabledFacet ])return (<fast-div className ={className }><fast-text text ={labelFacet } /></fast-div >)}
Using Equality Checks
Provide an equality check to optimize updates for complex data types:
tsxuseFacetWrapMemo ,useFacetMap ,shallowObjectEqualityCheck ,FacetProp } from '@react-facet/core'typeUserData = {name : stringage : number}constUserDisplay = ({user }: {user :FacetProp <UserData > }) => {// Use equality check to prevent updates when object properties haven't changedconstuserFacet =useFacetWrapMemo (user ,shallowObjectEqualityCheck )constuserName =useFacetMap ((u ) =>u .name , [], [userFacet ])return (<div ><fast-text text ={userName } /></div >)}
Performance Considerations
When useFacetWrapMemo helps:
- Prevents unnecessary downstream re-renders when facet references change
- Reduces memory allocation from creating new facet instances
- Maintains consistent subscription behavior
When useFacetWrap is sufficient:
- Simple components where facet reference changes don't matter
- Props that change infrequently
- No performance issues observed with current approach
Key Differences from useFacetWrap
| Feature | useFacetWrap | useFacetWrapMemo | 
|---|---|---|
| Facet reference stability | New facet on value change | Stable facet reference | 
| Value updates | New facet with new value | Same facet, updated internal value | 
| Memory allocation | New facet instance per value change | One facet instance, reused | 
| Best for | Simple wrapping, infrequent changes | Stable references, frequent value changes | 
| Performance overhead | Lower (simple memoization) | Slightly higher (effect subscriptions) | 
See Also
- useFacetWrap - Simpler version without stable references
- Equality Checks - Available equality check functions