reactjs - findDOMNode 在 StrictMode 中已弃用。findDOMNode 被传递了一个 Transition 的实例(由 MUI Backdrop 创建),它位于 StrictMode 内部
问题描述
我收到以下警告:
警告:在 StrictMode 中不推荐使用 findDOMNode。findDOMNode 被传递了一个在 StrictMode 中的 Transition 实例。相反,直接将 ref 添加到要引用的元素。在此处了解有关安全使用 refs 的更多信息:
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)) (at CircularProgressOverlay.tsx:20) in CircularProgress (at VpmsSpinnerMat.tsx:7) in VpmsSpinner (at ProductCreate.tsx:19) in div (at ProductCreate.tsx:18) in ProductCreate (created by Context.Consumer) in Route (at ProductMain.tsx:36) in Switch (at ProductMain.tsx:34) in Router (created by HashRouter) in HashRouter (at ProductMain.tsx:33) in Provider (at ProductMain.tsx:32) in ProductMain (at App.tsx:9) in div (created by Styled(MuiBox)) in Styled(MuiBox) (at Box.tsx:32) in Box (at App.tsx:8) in App (at src/index.tsx:9) in StrictMode (at src/index.tsx:8)
这是组件:
import React from "react";
import './spinner.css';
import {Backdrop, Theme} from "@material-ui/core";
import {createStyles, makeStyles} from "@material-ui/core/styles";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
backdrop: {
zIndex: theme.zIndex.drawer + 1,
color: '#fff',
},
}),
);
export default function CircularProgress(props: any) {
const classes = useStyles();
const wrapper= React.createRef();
return (
<Backdrop className={classes.backdrop} open={true} ref={wrapper}>
<div className="lds-dual-ring"/>
</Backdrop>
)
}
findDOMNode
是由 MUI 中的 Transition.js(由 Backdrop 创建)引起的。有没有办法在不禁用 StrictMode 的情况下删除警告?我试过了ref={wrapper}
,但没有效果。
解决方案
我应用了这个解决方案:
import React from "react";
import './spinner.css';
import {Backdrop, Theme} from "@material-ui/core";
import {createStyles, makeStyles, unstable_createMuiStrictModeTheme, ThemeProvider} from "@material-ui/core/styles";
// FIXME. To be fixed in future versions of MUI. Not advised for use in Production: https://material-ui.com/customization/theming/
const theme = unstable_createMuiStrictModeTheme();
const useStyles = makeStyles((theme: Theme) =>
createStyles({
backdrop: {
zIndex: theme.zIndex.drawer + 1,
color: '#fff',
},
}),
);
export default function CircularProgress(props: any) {
const classes = useStyles();
return (
<ThemeProvider theme={theme}>
<Backdrop className={classes.backdrop} open={true}>
<div className="lds-dual-ring"/>
</Backdrop>
</ThemeProvider>
)
}
推荐阅读
- mongodb - Azure 数据工厂,Mongo DB 连接字符串具有特殊字符
- c# - 是否可以使用 MOQ 框架模拟将“SqlDataReader”作为返回类型的 ExecuteReader 方法?
- html - 表单外的哪些内容可能会破坏隐式表单提交(Enter 键)?
- python-3.x - 如何从通过python中的kafka摄取数据的德鲁伊获取数据?
- cplex - 为什么我在使用 cvxpy 时会在 cp.installed_solvers() 中看到两个 cplex 求解器?
- angularjs - ctrl in undefined in angular 指令
- php - AWS RDS 和 PDO 不起作用 SQLSTATE[HY000] [2002]
- c# - 以度为单位的欧拉角的旋转矩阵
- actionscript-3 - 当我没有看到任何重复时如何修复重复的功能错误
- java - 如何在 Ubuntu 18.04 上安装最新的 openjdk 12