Is there a way to get ref
from a React 16 portal. I tried the following approach but it doesn't seem to work:
const Tooltip = props => (
ReactDOM.createPortal(
<div>{props.children}</div>,
// A DOM element
document.body
)
);
class Info extends React.Component {
render() {
return (
<Tooltip
ref={ el => this.tooltip = el }
>
My content
</Tooltip>
);
}
componentDidMount() {
console.log(this.tooltip); // undefined
}
}
I need the ref
in order to dynamically calculate the element final position!
document.body
a valid dom element? – ShortbreadReactDOM.createPortal
returns returns a ReactPortal instance, which is a valid ReactNode but not a valid DOM element – Shortbread