119
votes

I have a simple ReactJS app based on hooks (no classes) using StrictMode.

I am using React version 16.13.1 and Material-UI version 4.9.10.

In the Appbar I am using Drawer.

    <div className={classes.root}>
        <AppBar position="static">
            <Toolbar>
                <IconButton
                    edge="start"
                    className={classes.menuButton}
                    color="inherit"
                    aria-label="menu"
                    onClick={handleDrawerOpen}>
                    <MenuIcon />
                </IconButton>
                <Typography variant="h6" className={classes.title}>
                    Online Information
                </Typography>
            </Toolbar>
        </AppBar>
        <Drawer
            variant="persistent"
            anchor="left"
            open={open}
        ></Drawer>
    </div>

I notice that when I open the Drawer, I get the following warning.

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance 
of 
Transition which is inside StrictMode. Instead, add a ref directly to the element you 
want to reference. Learn more about using refs safely ....
in div (created by Transition)
in Transition (created by ForwardRef(Fade))
in ForwardRef(Fade) (created by ForwardRef(Backdrop))
in ForwardRef(Backdrop) (created by WithStyles(ForwardRef(Backdrop)))
in WithStyles(ForwardRef(Backdrop)) (created by ForwardRef(Modal))
in div (created by ForwardRef(Modal))
in ForwardRef(Portal) (created by ForwardRef(Modal))
in ForwardRef(Modal) (created by ForwardRef(Drawer))
in ForwardRef(Drawer) (created by WithStyles(ForwardRef(Drawer)))

I found some reference on the web for this issue but still can’t figure out how to resolve this issue.

Can someone please add some workaround for this problem?

Thank you

6
File an issue with Material-UI. Or fork the repo, fix it, issue PR, use your fork till PR is merged or issue is deprecated. It's just a warning for now, in third-party code.Drew Reese
Here's the issue that discusses this: github.com/mui-org/material-ui/issues/13394Ryan Cogswell
Thank you for the linkangus
Also, note that "StrictMode is enabled by default when using ReactDOM.createRoot and/or React.ConcurrentMode.". there are some good discussions here: github.com/styled-components/styled-components/issues/2154Mahdi Abdi
I also get this error when I use the Tooltip component and pass a custom component to the title props instead of passing a simple string!!!Alireza Kavian

6 Answers

55
votes

Yeah it's annoying. Material UI's team is not keeping up with the React devs. For now, just remove the Strict mode tag. It's unfortunately what happens with cutting edge technologies.

210
votes

According to Material-ui changelog, it should be solve in V5, which is still in alpha.

It seems that at least in some cases this issue cause by createMuiTheme. You can solve this issue by using the experimental (unstable) theme creator

If you want to get the experimental theme creator instead of removing React.StrictMode, you can change it's import from:

import { createMuiTheme } from '@material-ui/core';

To

import { unstable_createMuiStrictModeTheme as createMuiTheme } from '@material-ui/core';

35
votes

This is a StrictMode Warning

Strict mode checks are run in development mode only; they do not impact the production build.

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

to

ReactDOM.render(
    <App />,
  document.getElementById('root')
);
2
votes

change your theme configuration

import { createMuiTheme } from '@material-ui/core';

to

import { unstable_createMuiStrictModeTheme as createMuiTheme } from '@material-ui/core';

Generates a theme that reduces the amount of warnings inside React.StrictMode like Warning: findDOMNode is deprecated in StrictMode.

WARNING: Do not use this method in production.

for production use import { createMuiTheme } from '@material-ui/core'; and replace StrictMode to Fragment.

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

to

ReactDOM.render(
  <React.Fragment>
    <App />
  </React.Fragment>,
  document.getElementById('root')
);
1
votes

This warning is due to the Transition component which is used in many of the material-ui components like Drawer, Tooltip, Snackbar etc.

Personally, I faced this warning in all of them, but only fixed this for the Snackbar component.

The solution is to create a ref and pass it into your root component. Then, manually forward the ref to your child components which use Transition.

Here is the code for the Snackbar component which fixed the issue for me. Since it's just a warning, probably ignore it. You don't need to remove StrictMode. It will be fixed in future material-ui releases.

import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';

//MUI Stuff
import { makeStyles } from '@material-ui/core/styles';
import Snackbar from '@material-ui/core/Snackbar';
import MuiAlert from '@material-ui/lab/Alert';

// Redux
import { hideAlert } from '../../redux/actions/uiActions';
import Slide from '@material-ui/core/Slide';

const Alert = React.forwardRef((props, ref) => {
    return <MuiAlert ref={ref} elevation={6} variant="filled" {...props} />;
});

const SlideTransition = React.forwardRef((props, ref) => {
    return <Slide ref={ref} {...props} direction="left" />;
});

const useStyles = makeStyles((theme) => ({
    root: {
        flexGrow: 1,
    },
    snackbar: {
        [theme.breakpoints.down('sm')]: {
            bottom: 65,
        },
    },
}));

const SnackAlert = () => {
    const snackbarRef = React.createRef(null);
    const classes = useStyles();
    const { alert, alertType, alertMessage } = useSelector((state) => ({
        alert: state.ui.alert,
        alertType: state.ui.alertType,
        alertMessage: state.ui.alertMessage,
    }));
    const dispatch = useDispatch();
    const [open, setOpen] = React.useState(false);

    useEffect(() => {
        setOpen(alert);
    }, [alert]);

    const handleClose = () => {
        setOpen(false);
        dispatch(hideAlert());
    };

    return (
        <div className={classes.root}>
            <Snackbar
                ref={snackbarRef}
                className={classes.snackbar}
                open={open}
                anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
                autoHideDuration={5000}
                onClose={handleClose}
                message={alertMessage}
                TransitionComponent={SlideTransition}
            >
                <Alert onClose={handleClose} severity={alertType}>
                    {alertMessage}
                </Alert>
            </Snackbar>
        </div>
    );
};
export default SnackAlert;
-1
votes

I ran into this same error when trying to make a selection using the React material Select component. I found this page in the React documentation that talks about Strict Mode and references this particular error.

React StrictMode This error is referenced in the section:

Warning about deprecated findDOMNode usage

Here is an example snippet that indicates how to resolve the issue. Looks like we need to create a React Ref and then attach that ref to the DOM node that is throwing the error.

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.wrapper = React.createRef();
  }
  render() {
    return <div ref={this.wrapper}>{this.props.children}</div>;
  }
}