gpt4 book ai didi

reactjs - MUI 对话框 - 无法将 onClose 传递给对话框操作中的 onClick

转载 作者:行者123 更新时间:2023-12-02 16:04:51 24 4
gpt4 key购买 nike

我正在尝试创建一个可重复使用的 Dialog基于 MUI 对话框的组件。

这是我的代码:

import React from 'react';
import {
Dialog as MuiDialog,
DialogProps,
Button,
DialogContent,
DialogActions,
DialogTitle,
} from '@material-ui/core';

const Dialog = ({ title, open, onClose, children, ...props }: DialogProps) => {

return (
<MuiDialog
onClose={onClose}
aria-labelledby='simple-dialog-title'
open={open}
>
<DialogTitle id='simple-dialog-title'>{title}</DialogTitle>
<DialogContent>{children}</DialogContent>
<DialogActions>
<Button onClick={onClose} color='primary'>
Close
</Button>
</DialogActions>
</MuiDialog>
);
};

它一直在 <Button onClick={onClose} color='primary'> 处显示错误

错误:

No overload matches this call.
Overload 1 of 3, '(props: { href: string; } & { children?: ReactNode; color?: Color | undefined; disabled?: boolean | undefined; disableElevation?: boolean | undefined; disableFocusRipple?: boolean | undefined; ... 5 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>): Element', gave the following error.
Type '((event: {}, reason: "backdropClick" | "escapeKeyDown") => void) | undefined' is not assignable to type 'MouseEventHandler<HTMLAnchorElement> | undefined'.
Type '(event: {}, reason: "backdropClick" | "escapeKeyDown") => void' is not assignable to type 'MouseEventHandler<HTMLAnchorElement>'.
Overload 2 of 3, '(props: { component: ElementType<any>; } & { children?: ReactNode; color?: Color | undefined; disabled?: boolean | undefined; disableElevation?: boolean | undefined; ... 6 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>): Element', gave the following error.
Property 'component' is missing in type '{ children: string; onClick: ((event: {}, reason: "backdropClick" | "escapeKeyDown") => void) | undefined; color: "primary"; }' but required in type '{ component: ElementType<any>; }'.
Overload 3 of 3, '(props: DefaultComponentProps<ExtendButtonBaseTypeMap<ButtonTypeMap<{}, "button">>>): Element', gave the following error.
Type '((event: {}, reason: "backdropClick" | "escapeKeyDown") => void) | undefined' is not assignable to type 'MouseEventHandler<HTMLButtonElement> | undefined'.
Type '(event: {}, reason: "backdropClick" | "escapeKeyDown") => void' is not assignable to type 'MouseEventHandler<HTMLButtonElement>'. TS2769

22 | <DialogContent>{children}</DialogContent>
23 | <DialogActions>
> 24 | <Button onClick={onClose} color='primary'>
| ^
25 | Close
26 | </Button>
27 | </DialogActions>

这里我使用对话框

const SimpleDialogDemo = () => {
const [open, setOpen] = React.useState(false);

const handleClickOpen = () => {
setOpen(true);
};

const handleClose = (value: string) => {
setOpen(false);
};

return (
<div>
<br />
<Button variant='outlined' color='primary' onClick={handleClickOpen}>
Open simple dialog
</Button>
<Dialog open={open} onClose={handleClose} children={<div>Test</div>} />
</div>
);
}

最佳答案

来自docs ,这是 DialogonClose 属性的签名:

(event: object, reason: string) => void

这是 ButtononClick 回调签名:

(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void

这里的问题是您将第一个参数作为字符串传递给函数,这会导致 Button 中出现类型错误。从您的 handleClose 函数定义来看,您似乎想要收到关闭原因,您可以通过扩展 DialogProps 类型来实现:

import {
Dialog as MuiDialog,
DialogProps as MuiDialogProps
} from '@material-ui/core';

type CloseReason = 'backdropClick' | 'escapeKeyDown' | 'closeButtonClick';

interface DialogProps extends MuiDialogProps {
onClose: (reason: CloseReason) => void;
}

然后更新handleClose参数类型:

const handleClose = (value: CloseReason) => {
setOpen(false);
};

然后在 DialogButton 中像这样传递下去:

<MuiDialog onClose={(_, reason) => onClose(reason)}
<Button onClick={() => onClose('closeButtonClick')}

Codesandbox Demo

关于reactjs - MUI 对话框 - 无法将 onClose 传递给对话框操作中的 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69753753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com