Overview
usePresence allows components to access information about whether they’re still present in the React tree when used within AnimatePresence. This is useful for controlling custom exit animations.
Import
Signature
Return Values
Outside AnimatePresence
Returns[true, null] when the component is not a child of AnimatePresence.
Present in tree
Returns[true] when the component is present in the React tree.
Removed from tree
Returns[false, safeToRemove] when the component has been removed but AnimatePresence is keeping it mounted.
Parameters
subscribe
Whether to subscribe to the AnimatePresence context.Usage
The hook is typically used to delay the removal of a component until a custom animation completes.Examples
Custom exit animation
Delayed removal
Custom animation library
Conditional animations
Multi-step exit
With Web Animations API
Related Hook: useIsPresent
For simpler use cases where you only need to know if the component is present:useIsPresent simply returns a boolean and doesn’t provide the safeToRemove callback. Use this when you don’t need to control when the component is actually removed.
Notes
- Must be used within a child of
AnimatePresence - Call
safeToRemove()when your exit animation completes - Don’t call
safeToRemove()ifisPresentistrue - The component stays mounted until
safeToRemove()is called - Use
useIsPresentif you only need to detect presence without controlling removal