简体   繁体   English

React确认模式和Redux中间件

[英]React confirm modal and redux middleware

I'm new to React and Redux also. 我也是React和Redux的新手。 I want to remove item from the list so I dispatch an action deleteSelectedItems then I use redux middleware to catch it and show confirm. 我想从列表中删除项目,所以我调度动作deleteSelectedItems然后使用redux中间件来捕获它并显示确认。 That looks like below: 如下所示:

Action: 行动:

export const deleteSelectedItems = () => {
  return {
    type: ActionTypes.ITEM.DELETE_SELECTED,
    payload: {
      confirm: {
        message: 'Are you sure you want to delete these selected items?'
      }
    }
  }
};

Middleware: 中间件:

const confirmMiddleware = store => next => action => {
  if (action.payload.confirm) {
    if (confirm(action.payload.confirm.message)) {
      next(action);
    }
  } else {
    next(action);
  }
};

Everything works well. 一切正常。 Now, I don't want to use confirm() to show confirm dialog, I want to use my own ConfirmDialog component instead. 现在,我不想使用confirm()来显示确认对话框,而是要使用自己的ConfirmDialog组件。 I found @Dan Abramov solution , that is great. 我发现@Dan Abramov解决方案 ,这很棒。 But I am confused how to integrate those together. 但是我很困惑如何将它们整合在一起。 I want to use confirmMiddleware to dispatch an action that show modal but I don't know how to handle when user click ok or cancel on modal. 我想使用confirmMiddleware来调度显示模式的动作,但是当用户单击“确定”或在模式上取消时,我不知道如何处理。 How can I do that? 我怎样才能做到这一点?

Redux middleware isn't really the right place for UI, it only really works in your existing implementation because window.confirm has magical powers and can stop the thread of execution. Redux中间件并不是真正适合UI的地方,它只能在您现有的实现中真正起作用,因为window.confirm具有强大的功能并且可以停止执行线程。

Instead I would recommend dispatching a separate action to open the confirm dialog, eg CONFIRM_DELETE_ITEMS which toggles a flag to indicate the dialog should be displayed then dispatch the DELETE_ITEMS action when the dialog confirm button has been clicked. 相反,我建议分派一个单独的操作以打开确认对话框,例如CONFIRM_DELETE_ITEMS ,它会切换一个标志以指示该对话框应显示,然后在单击对话框的确认按钮时分派DELETE_ITEMS操作。

eg 例如

function Root({ dispatch, confirmDeleteItems }) {
    return (
        <div>
            {confirmDeleteItems ? (
                <ConfirmDialog onConfirm={() => dispatch(deleteItems())} onDeny={() = dispatch(hideModal())} />
            ) : null}
            <button onClick={() => dispatch(confirmDeleteItems())}>Delete</button>
        </div>
    )
}

I managed to independently re-invent the modal management technique that Dan describes in that issue, and then pushed it a bit farther. 我设法独立地重新发明了Dan在该期中描述的模式管理技术,然后再将其推向更远。 I did a writeup of my approach at Implement a confirm modal using React & Redux .Quoting myself: 我在使用React&Redux的实现确认模式中写了自己的方法,引述我自己:

I have a central component that is responsible for displaying all currently open dialogs, in the proper layout order (ie, I can have "DialogB" on top of "DialogA", etc). 我有一个中央组件,负责以正确的布局顺序显示所有当前打开的对话框(即,我可以在“ DialogA”的顶部放置“ DialogB”,依此类推)。 The component that wants to trigger showing the dialog runs an action creator that dispatches a "SHOW_DIALOG" action, with the name of the dialog component in the payload, and arbitrary additional data attached to the action. 要触发显示对话框的组件运行一个动作创建器,该动作创建器将分派“ SHOW_DIALOG”动作,并在有效负载中添加对话框组件的名称,并将任意附加数据附加到该动作。 That data is added to the store, and the dialog managing component will pass that extra data to the dialog component as props when it's rendered. 该数据将添加到存储中,并且对话框管理组件将在渲染时将这些额外数据作为道具传递给对话框组件。

I've created some generic "picker" dialogs, like ColorPicker and IconPicker. 我创建了一些通用的“选择器”对话框,例如ColorPicker和IconPicker。 These dialogs know nothing about the rest of my app. 这些对话框对我的应用程序的其余部分一无所知。 They can take some optional bits of data in their props (such as the initially selected color value), and are also looking for a special prop with a name like "onColorSelected". 他们可以在其属性中获取一些可选的数据位(例如最初选择的颜色值),并且还在寻找名称类似于“ onColorSelected”的特殊属性。 The component that requested the dialog can include the entirety of another action as part of the payload, and when the dialog has its "OK" button clicked on, that new action will be dispatched along with the "return value" of the dialog. 请求对话框的组件可以包括另一个动作的全部内容,作为有效负载的一部分,并且当单击对话框的“确定”按钮时,该新动作将与对话框的“返回值”一起分派。

So, in general, my suggestion is to include a plain action object that gets passed along to the dialog, and the dialog can dispatch a copy of the action when it is closed. 因此,总的来说,我的建议是包括一个传递给对话框的普通操作对象,并且当对话框关闭时,对话框可以调度该操作的副本。

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

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