【问题标题】:in react-redux and redux-saga what's a proper method of transitioning to a new route after an async call succeeds?在 react-redux 和 redux-saga 中,异步调用成功后转换到新路由的正确方法是什么?
【发布时间】:2018-03-14 17:33:59
【问题描述】:

我正在使用 react、redux、redux-saga 和 react-router-redux。我正在寻找一种在异步调用成功后转换到新路由的“正确”方式。

例如,用户提交订单并被重定向到感谢页面。在幕后,我们

  • 当前路线是orders/new
  • dispatch(submitOrder())
  • submitOrder() saga 运行,异步网络调用
  • api 返回成功,SUBMIT_ORDER_SUCCESS action 被调度

此时我想转换到orders/:orderId/complete

在哪里/如何正确执行此操作?

使用OrderFormContainer.componentWillReceiveProps() 钩子是可能的,检查像nextProps.submitDidSucceed 这样的布尔值,然后调用一个转换动作——但这​​很脆弱,感觉根本上是错误的。

【问题讨论】:

标签: reactjs react-redux redux-form redux-saga react-router-redux


【解决方案1】:

您可以在 saga 中使用 push 动作创建器导航到新位置:

import { push } from 'react-router-redux';

 //... your saga code
 // api return success
 yield put(SUBMIT_ORDER_SUCCESS); // your existing action
 yield put(push('/orders/:orderId/complete')); //  use push to redirect to desired location

【讨论】:

  • 我不确定从 saga 中进行路由是否合适,但是通过更多阅读和使用 react-router-redux,我将路由器视为另一个应用程序状态。我还将在传递给 saga 的操作中添加一个可选的 redirect 道具,并在 put(push()) 中使用它
猜你喜欢
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 2017-06-14
  • 2019-12-11
  • 2015-12-13
  • 2018-01-14
相关资源
最近更新 更多