Memo is a React component that memoizes its children and optionally creates a scoped tracking context for computed values.
Usage
Signature
Props
The content to render. Can be:
- A function that returns React elements
- An observable that resolves to React elements
- Any renderable React node
Controls the memoization behavior:
false(default): Memoizes based on children identity. Won’t re-render unless the children prop reference changes.true: Creates a fresh scope on each render. Useful when the children function needs to access updated local state or props.
Returns
The rendered result of the children.
Examples
Basic memoization
With observable children
Computed values
Scoped memo with local state
Default (unscoped) behavior
Multiple computed sections
Behavior
Memoization strategy
Memo is implemented using React.memo with a custom comparison function:
- When
scoped={false}(default): Only re-renders when thechildrenprop reference changes - When
scoped={true}: Creates a new scope on each parent render
Computed component
Internally,Memo uses the Computed component, which:
- Tracks observable access in the children function
- Re-evaluates when tracked observables change
- Uses
useSelectorwithskipCheck: truefor optimization
Performance characteristics
Comparison with observer
Memo
- Inline computed sections within a component
- Useful for isolating expensive computations
- Children must be a function or observable
- Fine-grained control over what’s memoized
observer
- Wraps entire component
- Simpler for components that fully rely on observables
- Works with normal JSX syntax
- Automatic tracking throughout component
When to use Memo
Use Memo when:
- You want to memoize a specific section of a component
- You have expensive computations derived from observables
- You want to prevent re-renders of a computed section when parent re-renders
- You need fine-grained control over reactive sections
Use observer instead when:
- The entire component relies on observables
- You want simpler, cleaner code
- You want automatic tracking without explicit scoping
Use useMemo instead when:
- You’re computing from non-observable values
- You need standard React memoization
Type definitions
Notes
Memois a memoized version of the internalComputedcomponent- When
scoped={false}, the comparison function checks ifchildrenprop changed - When
scoped={true}, the comparison always returnstrue(allows re-render) - Children functions should be pure (no side effects)
- For side effects, use
useObserveinstead
Related
- observer - Wrap entire components with tracking
- Computed - Internal component used by Memo
- Show - Conditional rendering with observables
- useSelector - Hook for tracking observables