反应,redux 链接下一个调度 [英] react, redux chain a next dispatch

查看:51
本文介绍了反应,redux 链接下一个调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然开始使用 react 和 redux,现在我遵循了这个 教程,它使用这个中间件 用于调度.我想知道在第一个调度之后我将如何进行另一个调度(链接它)?我现在有这样的事情.

fetchData() {const { dispatch } = this.props;const action = PageActions.fetchPage({slug: this.props.params.slug});调度(动作);}

并想知道我是否可以 dispatch(action).then(...)dispatch 的返回总是未定义的.这可能吗?

解决方案

如果你想在你的动作创建者内部使用异步动作,你需要使用中间件.推荐的中间件是 thunk.

Stack 上有一篇关于它的用法和适当情况的好帖子.堆栈溢出异步操作>

这将允许您从单个操作中分派多个操作.但是,如果您想将动作链接"在一起,您只需在第一个动作定义的末尾分派第二个动作.

function getBookings() {返回 (RequestHelper.fetchEntities(Routes.bookings.all, {}, queryParams).then(res => dispatch(getBookingsSuccess(res));)}...函数 getBookingsSuccess(data) {调度(显示成功());}

I'm still beginning in react and redux and now I followed this tutorial and it uses this middleware for dispatch. I was wondering how I would do another dispatch after the first one (to chain it)? I have something like this now.

fetchData() {
  const { dispatch } = this.props;
  const action = PageActions.fetchPage({slug: this.props.params.slug});
  dispatch(action);
}

and wondering if I can dispatch(action).then(...) but the return of dispatch is always undefined. Is that possible?

解决方案

If you would like to use async actions inside of your action creators you need to use middleware. The recommended middleware is thunk.

There is a good post on Stack about it's usage and appropriate situations. Stack Overflow Async Actions

This will allow you to dispatch many actions from a single action. However if you are wanting to "chain" actions together you should simply dispatch the second action at the end of the first actions definition.

ie

function getBookings() {
  return (
    RequestHelper.fetchEntities(Routes.bookings.all, {}, queryParams)
      .then(res => dispatch(getBookingsSuccess(res));
  )
}

...
function getBookingsSuccess(data) {
  dispatch(showSuccess());
}

这篇关于反应,redux 链接下一个调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆