简体   繁体   English

findDOMNode 在 StrictMode React 材质 UI 中已弃用

[英]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.

相关问题 按钮 onClick 在反应中不起作用,显示 FindDOMNode 在 StrictMode 中已弃用 - Button onClick is not is not working in react displaying FindDOMNode is deprecated in StrictMode 什么是 findDOMNode,它在控制台的 StrictMode 中已被弃用? - What is findDOMNode and it is deprecated in StrictMode in console? 这是什么意思? 警告:在 StrictMode 中不推荐使用 findDOMNode - What does this mean? Warning: findDOMNode is deprecated in StrictMode findDOMNode 在 StrictMode 中被弃用如何使用 refs 代替 - findDOMNode is deprecated in StrictMode how to use refs instead findDOMNode 在 StrictMode 中被弃用。 警告 - react-transition-group + react v17 + Javascript(不是 Typescript) - findDOMNode is deprecated in StrictMode. Warning - react-transition-group + react v17 + Javascript (Not Typescript) 反应路由器 findDomNode 已弃用 - React router findDomNode is deprecated 警告:在 StrictMode 中不推荐使用 findDOMNode。 findDOMNode 传递了一个在 StrictMode 内的 Transition 实例 - Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Transition which is inside StrictMode findDOMNode 在 StrictMode 中已弃用。 findDOMNode 传递了一个 DraggableCore 实例,它在 StrictMode 中 - findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of DraggableCore which is inside StrictMode 如何修复“在 StrictMode 中不推荐使用 findDOMNode”错误? - How can I fix ''findDOMNode is deprecated in StrictMode'' error? 将工具提示添加到输入会导致 `findDOMNode is deprecated in StrictMode` 错误 - Ant 设计 - Adding Tooltip to Input causes `findDOMNode is deprecated in StrictMode` error - Ant Design
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM