[英]findDOMNode is deprecated in StrictMode React material UI
I am getting this error on my React material UI project我的 React 材质 UI 项目出现此错误
By looking at the error I guess it comes somewhere inside the Drawer.js
component.通过查看错误,我猜它出现在
Drawer.js
组件中的某个地方。 This is my full Drawer.js
component这是我完整的
Drawer.js
组件
import React, { Fragment, useState } from "react"; import clsx from "clsx"; import { makeStyles, useTheme } from "@material-ui/core/styles"; import Drawer from "@material-ui/core/Drawer"; import { List, Collapse } from "@material-ui/core"; import Divider from "@material-ui/core/Divider"; import ListItem from "@material-ui/core/ListItem"; import IconButton from "@material-ui/core/IconButton"; import ListItemText from "@material-ui/core/ListItemText"; import { toggleDrawer } from "../../store/actions/authActions"; import ExpandLess from "@material-ui/icons/ExpandLess"; import ExpandMore from "@material-ui/icons/ExpandMore"; import ChevronLeftIcon from "@material-ui/icons/ChevronLeft"; import ChevronRightIcon from "@material-ui/icons/ChevronRight"; // import ClickAwayListener from '@material-ui/core/ClickAwayListener' import { useHistory } from 'react-router-dom'; import { connect } from "react-redux"; import { withRouter } from "react-router"; const useStyles = makeStyles({ list: { width: 250, }, fullList: { width: "auto", }, }); function TemporaryDrawer(props) { const classes = useStyles(); const theme = useTheme(); // const [open, setOpen] = React.useState(false); const [openIndex, setOpenIndex] = useState(0); let history = useHistory(); // const handleDrawerOpen = () => { // setOpen(true); // }; const handleDrawerClose = () => { props.toggleDrawer(); }; const handleClick = (index) => { if (openIndex === index) { setOpenIndex(-1); } else { setOpenIndex(index); } }; const onToggle = () => (event) => { if ( event.type === "keydown" && (event.key === "Tab" || event.key === "Shift") ) { return; } props.toggleDrawer(); }; const onRoute = (path) => { // props.history.push(path); history.push(path); props.toggleDrawer(); }; const list = (anchor) => ( <div className={clsx(classes.list, { [classes.fullList]: anchor === "top" || anchor === "bottom", })} role="presentation" > <div className={classes.drawerHeader}> <IconButton onClick={handleDrawerClose}> {theme.direction === "ltr" ? ( <ChevronLeftIcon /> ) : ( <ChevronRightIcon /> )} </IconButton> </div> <Divider /> <List> {props.permissions.map((route, index) => ( <Fragment key={index}> <ListItem button onClick={(e) => handleClick(index)}> <ListItemText primary={route.name} /> {index === openIndex ? <ExpandLess /> : <ExpandMore />} </ListItem> {route.children.length && ( <Collapse in={openIndex === index ? true : false} timeout="auto" unmountOnExit > <List component="div" disablePadding> {route.children.map((child, idx) => ( <ListItem button className={classes.nested} key={idx} onClick={() => onRoute(child.path)} > <ListItemText primary={child.name} /> </ListItem> ))} </List> <Divider /> </Collapse> )} </Fragment> ))} </List> {/* <Divider /> */} </div> ); return ( <div> {props.token && ( <Drawer anchor="left" open={props.isDrawerOpen} onClose={onToggle("left", false)} variant="persistent" > {list("left")} </Drawer> )} </div> ); } const mapStateToProps = (state) => { return { isDrawerOpen: state.auth.isDrawerOpen, token: state.auth.token, permissions: state.auth.routes, }; }; export default withRouter( connect(mapStateToProps, { toggleDrawer })(TemporaryDrawer) );
I go through this error and some say this is a problem in MUI
library and no way to fix this.我经历了这个错误,有人说这是
MUI
库中的一个问题,无法解决这个问题。 But I believe there must be a workaround for this.但我相信必须有一个解决方法。 This causes serious problems for UI.
这会导致 UI 出现严重问题。
Where this error comes from and what can I do to fix this?这个错误来自哪里,我能做些什么来解决这个问题?
Any help!任何帮助!
Thanks in advance.提前致谢。 =)
=)
I use material UI 4.11.0
and react 16
我使用材料 UI
4.11.0
并反应16
You only need to create a new childComponent with react.forwardRef passing respective props and refs:您只需要使用 react.forwardRef 传递各自的 props 和 refs 创建一个新的 childComponent :
const ChildComponent = React.forwardRef((props, ref) =>
<div ref={ref}>
<yourChildcomponent {...props} />
</div>
);
In your render change the name of the original Child for the new:在您的渲染中,为新更改原始 Child 的名称:
<ChildComponent... />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.