I'm trying to obtain a reference to the DOM Node of a material-ui Dialog the first time my component loads, but the reference is always undefined.
Here's my approach:
import React, { useEffect, useRef } from "react";
import {
Dialog,
DialogTitle,
DialogContent,
DialogContentText
} from "@material-ui/core";
export default function App() {
const dialogRef = useRef();
useEffect(() => {
console.log("Dialog Ref");
console.log(dialogRef.current);
}, []);
return (
<Dialog open ref={dialogRef}>
<DialogTitle>Ref Test Dialog</DialogTitle>
<DialogContent>
<DialogContentText>
Please check the console to see if a ref to this Dialog was logged.
</DialogContentText>
</DialogContent>
</Dialog>
);
}
The Material UI Dialog API docs state that "the ref is forwarded to the root element," and my approach is based on the React useRef docs, so it seems like this should be working.
In fact, when I use the same approach on a <div> element, it does work. Am I missing something, or is this a bug in Material UI?
Here's a "working" example of the issue:
https://codesandbox.io/s/hopeful-agnesi-3i0p3?file=/src/App.js