Skip to content

Instantly share code, notes, and snippets.

@karpolan
Created July 26, 2020 21:29
Show Gist options
  • Save karpolan/68678dfc79f05edbced607ca024e1362 to your computer and use it in GitHub Desktop.
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()"
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