Created
July 26, 2020 21:29
-
-
Save karpolan/68678dfc79f05edbced607ca024e1362 to your computer and use it in GitHub Desktop.
React <InvisibleRef /> component to avoid "Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
/** | |
* Renders "invisible" div to use as a first element in Material UI Menu, and other popup components who obtain | |
* a DOM ref from the child component. This allows to avoid: "Warning: Function components cannot be given refs. | |
* Attempts to access this ref will fail. Did you mean to use React.forwardRef()" | |
* @class InvisibleRef | |
*/ | |
const InvisibleRef = React.forwardRef((props, ref) => { | |
const styleInvisible = { | |
height: 0, | |
padding: 0, | |
margin: 0, | |
} | |
return <div ref={ref} style={styleInvisible} {...props} /> | |
}) | |
export default InvisibleRef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment