简体   繁体   English

使用 React & Redux 制作确认模态

[英]Make a Confirmation Modal using React & Redux

I was making an application in which I need to make a confirmation box to ask the user if he wants to delete the record or not.我正在制作一个应用程序,我需要在其中制作一个确认框来询问用户是否要删除记录。 I want to use the confirmation box globally .我想在全球范围内使用确认框

Suppose I want to delete a record, when the user clicks the delete button it executes the action to delete the record.假设我要删除一条记录,当用户点击删除按钮时,它会执行删除记录的操作。 But once inside the delete action another action is dispatched to show Modal.但是一旦进入删除操作,就会调度另一个操作来显示模态。 (till this point I have reached) (直到这一点我已经达到)
However, I'm struggling on the part where execution should wait for the user to click confirm or cancel.但是,我在执行应该等待用户单击确认或取消的部分上苦苦挣扎。 Once the user has performed the action then execution to delete the record should continue.一旦用户执行了操作,删除记录的执行就应该继续。

I hope my problem statement was clear.我希望我的问题陈述很清楚。

Please let me know what should I do.请让我知道我该怎么做。 I really want to learn how this thing works in React.我真的很想了解这个东西在 React 中是如何工作的。

Currently, I'm using a window's confirmation to ask the user Yes or No.目前,我正在使用窗口确认询问用户是或否。

// Delete Profile
export const deleteProfile = (history) => async (dispatch) => {
    try {
        if (
            window.confirm('Are you Sure? Your account would be permanently lost')
        ) {
            await axios.delete(`/api/profile`);

            dispatch({ type: actionTypes.CLEAR_PROFILE });
            dispatch({ type: actionTypes.ACCOUNT_DELETED });
            history.push('/login');
            dispatch(
                setAlert('Your account has been deleted permanently', 'success')
            );
        }
    } catch (err) {
        dispatch(setAlert('Profile deletion error', 'danger'));
    }
};

I'm hoping to call a modal in place of window confirmation and wait for user input, if it returns true I want to move ahead or else abort.我希望调用一个模式来代替 window 确认并等待用户输入,如果它返回 true 我想继续前进或者中止。

You have to create a Component called Dialog that opens when the deleteProfile method is called and put two buttons, one for canceling and the other for delete, when the delete button is pressed you call the deleting code.您必须创建一个名为 Dialog 的组件,它会在调用 deleteProfile 方法时打开,并放置两个按钮,一个用于取消,另一个用于删除,当按下删除按钮时,您将调用删除代码。 I recommend you to use some library like material-ui .我建议你使用像material-ui这样的库。 This library has multiple useful and very good looking components that you can utilize.该库有多个有用且非常漂亮的组件供您使用。 For this case in specific I recommend you the Dialog component.对于这种情况,我特别推荐您使用Dialog组件。 You can use it like this:你可以像这样使用它:

<Dialog
   open={dialogOpen}
   onClose={dialogClose}
   >
   <DialogContent>
      Are you Sure? Your account would be permanently lost
   </DialogContent>
   <DialogActions>
      <Button>Cancel</Button>
      <Button onClick={deleteMethod}>Delete</Button>
   </DialogActions>
</Dialog>

Where dialogOpen is either a boolean component state or a redux state when is true, the Dialog will open;其中dialogOpen是 boolean 组件 state 或 redux state 为真时,对话框将打开; and dialogClose is a method where you will change the dialogOpen state to false to the Dialog to close. dialogClose是一种方法,您可以在其中将dialogOpen state 更改为 false 以关闭对话框。

Now as you can check you have the deleteMethod where all you deleting code will be.现在你可以检查你有deleteMethod ,所有你删除的代码都将在其中。 You call this code when the delete button is pressed.按下删除按钮时调用此代码。

For open the Dialog asynchronusly what you can do is put and await once you set dialogOpen variable, something like this:要异步打开对话框,您可以在设置dialogOpen变量后放置并等待,如下所示:

await this.setState({dialogOpen: true});

or if you want inside an async method like this:或者如果你想要一个像这样的异步方法:

const method = async () =>{
  await this.setState({dialogOpen: true});
}

And you just have to call the method like this:你只需要像这样调用方法:

await method();

I hope this helps!我希望这有帮助!

Open confirmation box on click of delete button.单击删除按钮打开确认框。 Delete record on click of confirm button.单击确认按钮删除记录。

thanks.谢谢。 I tried this and it really worked.我试过了,它真的很管用。 how I didn't think of that.我怎么没想到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM