Overview
Refs provide a way to access DOM elements or store mutable values that persist across renders. Portals allow you to render components outside of the parent component’s DOM hierarchy.useRef Hook
Access DOM elements and store mutable values
forwardRef
Pass refs through components to children
useImperativeHandle
Expose custom component APIs to parent components
Portals
Render components outside the parent DOM tree
Why Use Refs?
Refs are useful when you need to:useRef Hook
Basic DOM Access
The most common use case for refs is accessing DOM elements.The
current property of a ref contains the actual DOM element after React mounts it. Always access DOM elements via ref.current.Storing Mutable Values
Refs can store any mutable value that shouldn’t trigger re-renders.State vs Refs
- useState
- useRef
- Triggers re-render when updated
- Asynchronous updates
- Immutable update pattern
- Use for UI-related data
- Form input values displayed in UI
- Toggle states (open/closed)
- Lists, counters, any displayed data
forwardRef
By default, you can’t pass refs to functional components.forwardRef enables this.
Basic Usage
forwardRef accepts a render function that receives props and ref as separate arguments. The component name should be provided as the function name for better debugging.useImperativeHandle
Customize the ref value exposed to parent components.Benefits of useImperativeHandle
Encapsulation
Encapsulation
- Hide internal implementation details
- Expose only necessary methods
- Prevent direct DOM manipulation by parents
- Create a clear component API
Flexibility
Flexibility
- Add validation or logic before DOM operations
- Combine multiple operations into one method
- Provide custom methods beyond native DOM APIs
- Maintain control over component behavior
Maintainability
Maintainability
- Change internal implementation without breaking parent components
- Create self-documenting component interfaces
- Easier to test and debug
Portals
Portals let you render components outside the parent component’s DOM hierarchy.Why Use Portals?
Modals & Dialogs
Render modals at document root to avoid z-index issues
Tooltips
Position tooltips correctly regardless of parent overflow
Dropdowns
Prevent clipping by parent containers
Notifications
Display notifications at a consistent location
Creating a Portal
Basic Portal Example
Advanced Portal with Refs
Combine portals with refs for maximum control.Even though portals render in a different DOM location, they still behave like normal React children in terms of event bubbling and context.
Event Bubbling with Portals
Portals maintain React’s event bubbling behavior.Clicking either button will trigger the parent’s
onClick handler, even though the Modal renders via a portal outside the parent’s DOM tree.Best Practices
When to use refs
When to use refs
DO use refs for:
- Managing focus, text selection, or media playback
- Triggering imperative animations
- Integrating with third-party DOM libraries
- Storing timer IDs, previous values, or other non-UI data
- Anything that should trigger a re-render
- Values displayed in the UI (use state instead)
- Communication between components (use props/context)
Ref timing
Ref timing
- Refs are
nullduring the initial render - Access
ref.currentonly after the component mounts - Use
useEffectif you need to run code when a ref is set - Never read or write refs during rendering
Portal best practices
Portal best practices
- Always provide a portal root in your HTML
- Use portals for modals, tooltips, and overlays
- Remember that CSS inheritance doesn’t cross portal boundaries
- Event handlers still work as if the portal was in the React tree
forwardRef considerations
forwardRef considerations
- Use meaningful names for forwarded components
- Combine with
useImperativeHandleto expose controlled APIs - Don’t overuse - not every component needs to forward refs
- Document what ref methods are available to consumers
Common Patterns
- Auto-focus Input
- Scroll to Element
- Previous Value
- Modal with Portal
Related Topics
Styling
Learn about different styling approaches
Animations
Add smooth transitions to your portals and modals