作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 react-router-navigation-prompt 在用户离开页面时显示模态窗口。它工作正常,但是在更新我的依赖项之后我不断收到此错误:
我尝试将 ref 转发给子组件,但它似乎没有用。这是我的代码:
import React from 'react';
import NavigationPrompt from 'react-router-navigation-prompt';
import { CancelChangesDialog } from './CancelChangesDialog';
import _ from 'lodash';
interface Props<T> {
currentForm: T;
initialForm: T;
}
export function LeavingPrompt<T>(props: Props<T>) {
const { currentForm, initialForm } = props;
return (
<NavigationPrompt when={!_.isEqual(currentForm, initialForm)}>
{({ onConfirm, onCancel }) => (
<CancelChangesDialog
open
handleCancel={onCancel}
handleConfirm={onConfirm}
/>
)}
</NavigationPrompt>
);
}
CancelChangesDialog 是模态视图组件:
import React from 'react';
import {
Dialog,
DialogTitle,
DialogContent,
DialogContentText,
DialogActions,
Button,
} from '@material-ui/core';
interface Props {
open: boolean;
handleCancel(): void;
handleConfirm(): void;
}
export function CancelChangesDialog(props: Props) {
const { open, handleCancel, handleConfirm } = props;
return (
<Dialog open={open}>
<DialogTitle id="alert-dialog-title">{'Confirm changes'}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Changes are not saved! Leave anyway?
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleCancel} color="primary">
Cancel
</Button>
<Button
onClick={handleConfirm}
color="primary"
style={{ textTransform: 'initial' }}
autoFocus
>
Confirm
</Button>
</DialogActions>
</Dialog>
);
}
有什么解决办法吗?
最佳答案
问题跟踪引用:https://github.com/mui-org/material-ui/issues/13394
有几个选项:
<Dialog disableStrictModeCompat>
import { ThemeProvider } from "@material-ui/core/styles";
import { unstable_createMuiStrictModeTheme } from "@material-ui/core/styles";
const theme = unstable_createMuiStrictModeTheme();
<ThemeProvider theme={theme}>
<Dialog>...</Dialog>
</ThemeProvider>
注意:阅读不稳定主题的文档。有局限性。
修复的Codesandbox演示: https://codesandbox.io/s/practical-gould-j7pqu
The API allows in many cases that users pass a custom root component. We still need findDOMNode if those components don't forward their refs properly.
关于javascript - 使用 react-router-navigation-prompt 时关于弃用 findDOMNode 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62250315/
我是一名优秀的程序员,十分优秀!