【问题标题】:Is it possible to share a promise between to sibling components via the redux store?是否可以通过 redux 存储在兄弟组件之间共享承诺?
【发布时间】:2018-06-29 09:37:36
【问题描述】:

我有两个组件,DeleteCards 和 ConfirmModal。 DeleteCards 组件显示可以删除的卡片。

当用户点击deleteCard 时,一个动作会像这样更新状态:

{ showModal : true, title : 'Delete card', body : 'Are you sure?'}

所以,这会导致我的模态窗口出现,要求用户确认。问题是我现在需要我的 deleteCard 函数等待并收听模式内的确认点击。

技术上我可以做到:

{ showModal : true, title : 'Delete card', body : 'Are you sure?', promise: promiseRef }

但是,我知道这是一种反模式。那么,我在这里有什么选择?或者完成这个的redux方式是什么?

【问题讨论】:

    标签: redux react-redux


    【解决方案1】:

    从技术上讲,这不是与 Redux 相关的问题,您可以将函数作为道具传递: <ConfirmModal onConfirm={this.onModalConfirm}/>

    或者,如果您 100% 决定使用 Redux,只需制作一个标志,类似于 modalConfirmed,将其添加到 mapStateToProps,在您的 componentDidUpdate 中,检查该道具,然后控制该功能你需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-04
      • 2017-10-11
      • 2018-02-17
      • 2021-01-11
      • 2017-10-01
      • 1970-01-01
      • 2018-03-10
      • 2018-04-05
      相关资源
      最近更新 更多