简体   繁体   English

有没有办法用 Redux 制作带有动作按钮的可重用模式?

[英]Is there a way to make reusable modal with action buttons with Redux?

I have a dialog message in a modal that asks the user to save changes if there is an attempt to navigate to another screen without saving.我在模态中有一条对话框消息,如果尝试导航到另一个屏幕而不保存,则要求用户保存更改。

It has 3 buttons ( Save , Proceed without Saving , and Close ).它有 3 个按钮( SaveProceed without SavingClose )。 Obviously, the modal window looks exactly the same on all screens but the Save and Go back without Saving perform different actions depending on the screen.显然,模式窗口在所有屏幕上看起来都完全相同,但Save and Go back without Saving根据屏幕执行不同的操作。

I tried creating a separate Redux reducer for the modal but I learnt that you aren't allowed to store functions in Redux.我尝试为模态创建一个单独的 Redux reducer,但我了解到您不允许在 Redux 中存储函数。

So my question is what is the recommended approach in a situation where you have the same modals in terms of UI but different in terms of the actions they perform?所以我的问题是,在 UI 方面具有相同模态但在它们执行的操作方面不同的情况下,推荐的方法是什么?

I would really want to have it at the root level so that I can just dispatch an action and not be bothered with importing the modal to each component and controlling its state inside the component.我真的希望在根级别拥有它,这样我就可以分派一个动作,而不必费心将模态导入每个组件并控制其在组件内的状态。

Here is an example of the action that I initially was planning to dispatch to show the modal这是我最初计划发送以显示模态的操作示例

        showModal: (state, { payload }) => {
            state.modal.isOpen = true;
            state.modal.header = payload.header;
            state.modal.title = payload.title;
            state.modal.btnText1 = payload.btnText1;
            state.modal.btnText2 = payload.btnText2;
            state.modal.btnText3 = payload.btnText3;
            state.modal.btnAction1 = payload.btnAction1;
            state.modal.btnAction2 = payload.btnAction2;
            state.modal.btnAction3 = payload.btnAction3;
        }

I would create a "wrapper" component like PageWithConfirmModal , and use it in each page component:我会创建一个“包装器”组件,如PageWithConfirmModal ,并在每个页面组件中使用它:

const MyCurrentPage = function( props ){

  return <PageWithConfirmModal
    showModal= { props.showModal }
    save=      { props.callSpecificSaveAction }
    proceed=   { props.callSpecificProceedAction }
    close=     { props.callSpecificCloseAction }
  >
    ... page content ...
  </PageWithConfirmModal>;
} 

It depends where your payload.btnAction1 etc. come from, but you will figure out how to adapt my example, I guess.这取决于你的payload.btnAction1等来自哪里,但你会弄清楚如何调整我的例子,我猜。

Alternatively there are several possible variants, depending on your situation, eg pass some property that describes what to do instead of the actions, and decide which action to use inside the PageWithConfirmModal or inside the modal, eg:或者,根据您的情况,有几种可能的变体,例如传递一些描述要做什么而不是动作的属性,并决定在PageWithConfirmModal或模态内部使用哪个动作,例如:

const MyCurrentPage = function( props ){

  return <PageWithConfirmModal
    showModal= { props.showModal }
    whatToDo=  { DO_ACTIONS_FROM_MAIN_PAGE }
  >
    ... page content ...
  </PageWithConfirmModal>;
} 
// e.g. inside PageWithConfirmModal.jsx
let actions;
if( DO_ACTIONS_FROM_MAIN_PAGE ){
  actions = {
    save:    callSpecificSaveAction,
    proceed: callSpecificProceedAction,
    close:   callSpecificCloseAction,
  }
} else if( ...

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

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