question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

  1. Create ref in component constructor with createRef
  2. Pass ref to Portal
  3. That’s it, Portal breaks and complains the container is not a valid element: image

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:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
desktpcommented, Apr 23, 2019

That’s my interpretation from

Note The examples below have been updated to use the React.createRef() API introduced in React 16.3. If you are using an earlier release of React, we recommend using callback refs instead.

In any case, the added explanation in the docs suffice, and my problem is solved. Thanks.

0reactions
eps1loncommented, Apr 23, 2019

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.

which is the recommended way of dealing with refs moving forward.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found