确认对话框没有打开 react-admin

问题描述

当使用 React Admin 单击按钮时,我试图打开一个确认对话框。按钮点击名称是“handleSendItemsToUpdate”。

但是对话框没有打开。

请在下面找到代码

  const notify = useNotify();
  const [open,setopen] = React.useState(false);
  const handleDialogClose = () => setopen(false);
  const handleSendItemsToUpdate = (props) => {

    const handleConfirm = () => {
        notify('Stuff is done.');
        setopen(false);
    };

    setopen(true);


    return (

        <Fragment>
            <SaveButton {...props} />
            <Confirm
                isOpen={open}
                title="Update View Count"
                content="Are you sure you want to reset the views for these items?"
                onConfirm={handleConfirm}
                onClose={handleDialogClose}
            />
        </Fragment>
    );
}

...
<Button className={classes.button} onClick={() => handleSendItemsToUpdate(props)}>Send Items To 
Update</Button>

感谢任何帮助。

提前致谢!

贝古姆

解决方法

Confirm 对话框在 handleSendItemsToUpdate 函数中返回,该函数未在组件中呈现(在 DOM 中使用),这就是它无法显示的原因。

可以把函数中的return 放到component的return中,当然只有open状态为true时才会显示。

您可以在这里查看我的演示:https://codesandbox.io/s/peaceful-dewdney-6pil2?file=/src/App.js