简体   繁体   English

在本机中处理多个调度

[英]Handle multiple dispatch in react-native

I would like some help with calling multiple dispatch. 我需要一些有关调用多个调度的帮助。 I have the below code but the dispatch calls are asynchronous. 我有以下代码,但分派调用是异步的。 I am not sure how to make synchronous dispatch calls? 我不确定如何进行同步调度调用? Below is my code. 下面是我的代码。

componentDidMount () {
    this.props.getItem1()
    this.props.getItem2()
    this.props.getItem3()
}
const mapStateToProps = state => ({
  item1Fetching: state.item1.fetching,
  item1Error: state.item1.error,
  item2Fetching: state.item2.fetching,
  item2Error: state.item2.error,
  item3Fetching: state.item3.fetching,
  item3Error: state.item3.error,
})

const mapDispatchToProps = dispatch => ({
  getItem1: () => {
    dispatch(Item1Actions.item1Request())
  },
  getItem2: () => {
    dispatch(Item2Actions.item2Request())
  },
  getItem3: () => {
    dispatch(Item3Actions.item3Request())
  },
})

export default connect(mapStateToProps, mapDispatchToProps)(LoadingScreen)

as a workaround you can create a single action to dispatch and in execution of that action you can dispatch your three item fetching methods one by one. 作为一种变通方法,您可以创建一个要分派的动作,在执行该动作时,可以一一分派三种获取方法。

in your mapDispatchToProps method you can do the following 在您的mapDispatchToProps方法中,您可以执行以下操作

const mapDispatchToProps = dispatch => bindActionCreators(itemsAction, dispatch)

in your actions.js you can do the following.... 在您的actions.js中,您可以执行以下操作...。

`export const itemsActions = () => {
  return (dispatch, getState) => {
    return item1Request().then((data) => {
      dispatch(data);
      return item2Request().then(data2 => {
        dispatch(data2);
        return item3Request().then(data3 => dispatch(data3))
      })
    })
  }
}`

I hope this helps you. 我希望这可以帮助你。 this way you can make them one by one 这样你就可以一一制作

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

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