Portal breaks when using createRef
See original GitHub issue- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior 🤔
Portal component should accept refs created through createRef()
Current Behavior 😯
Portal blows up when doing so
Steps to Reproduce 🕹
Link: https://codesandbox.io/s/pyp3p5r0kx
- Create ref in component constructor with
createRef - Pass ref to
Portal - That’s it,
Portalbreaks and complains the container is not a valid element:
Context 🔦
I’m upgrading my app to React 16 and doing performance optimizations, one of which is avoiding inline functions as props, as they get renewed every time the component updates and triggers re-renders down the whole tree.
Your Environment 🌎
| Tech | Version |
|---|---|
| Material-UI | v3.9.3 |
| React | v16.8.4 |
| Browser | |
| TypeScript | |
| etc. |
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Portal breaks when using createRef #15456 - mui/material-ui
Create ref in component constructor with createRef; Pass ref to Portal; That's it, Portal breaks and complains the container is not a valid ......
Read more >Refs and the DOM - React
createRef () receives the underlying DOM element as its current property. When the ref attribute is used on a custom class component, the...
Read more >How to get ref from a React 16 portal? - Stack Overflow
ReactDOM.createPortal returns a ReactPortal instance, which is a valid ReactNode but not a valid DOM element. At the same time createPortal ...
Read more >A complete guide to React refs - LogRocket Blog
When working with class-based components in the past, we used createRef() to create a ref. However, now that React recommends functional ...
Read more >6 Practical Applications for useRef - Frontend Digest
Before React hooks were released, we used to create refs using the createRef API. Since the release of React hooks, we can count...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

That’s my interpretation from
In any case, the added explanation in the docs suffice, and my problem is solved. Thanks.
With ref-objects you don’t know when the instance changes the ref points to which is required for a portal. React portals need a DOM element not a ref.
I’m not aware of such a blanket recommendation. The docs still have a section for callback refs.
The example I used can also be ported to ref-objects. However, it becomes increasingly complicated to know when the DOM instance is available if you forward the ref to another component.