Reactjs mapDispatchToProps-wrapper中的异步,以确保在渲染之前进行所有调度

Reactjs mapDispatchToProps-wrapper中的异步,以确保在渲染之前进行所有调度,reactjs,react-native,redux,react-redux,redux-thunk,Reactjs,React Native,Redux,React Redux,Redux Thunk,我希望的结果是,除非所有异步函数都已调度,否则我的组件不会呈现。我用它作为包装,以确保一切都已发送。我试过两种方法: 调用componentWillMount中的所有内容,并使用setState设置loaded=true。然后,我可以根据我的状态的已加载密钥渲染组件 ajax = async () => { try{ const { dispatch } = this.props; dispatch(loadPack('ars')); dispatch(load

我希望的结果是,除非所有异步函数都已调度,否则我的组件不会呈现。我用它作为包装,以确保一切都已发送。我试过两种方法:

  • 调用
    componentWillMount
    中的所有内容,并使用
    setState
    设置
    loaded=true
    。然后,我可以根据我的状态的已加载密钥渲染组件

    ajax = async () => {
      try{
        const { dispatch } = this.props;
        dispatch(loadPack('ars'));
        dispatch(loadPack('acr'));
        await dispatch(loadProds());
        await dispatch(loadRepls());
        await dispatch(checkEligibile());
        }catch (e) { console.log(e)}
     }
    
     componentWillMount() {
       this.ajax().then(() => {
         this.setState({ loaded: true });
       });
     }
    
    render() {
     const { loaded } = this.state;
     return loaded ? <Component/> : null;
    }
    

    因为您使用的是redux,所以您有一个全局redux状态。因此,在所有调度之后,再调度一个操作,使reducer状态变为true,这表示所有操作都已调度

    在组件中,使用dispatchStateToProps方法将减速器状态转换为道具,然后使用该道具检查是否已调度所有动作。大致应该是这样的

    ajax = async () => {
      try{
        const { dispatch } = this.props;
        dispatch(loadPack('ars'));
        dispatch(loadPack('acr'));
        await dispatch(loadProds());
        await dispatch(loadRepls());
        await dispatch(checkEligibile());
        // Add one more action here
        await dispatch(everythingDispatched());
        }catch (e) { console.log(e)}
     }
    
    render() {
     const { everythingDispatched } = this.props;
     return everythingDispatched ? <Component/> : null;
    }
    
    function mapStateToProps(state){
      return {
           everythingDispatched:state.dispatchTrackerReducer.everythingDispatche
      }
    }
    
    然后是一个保持跟踪的状态

    dispatchTrackerReducer.js
    switch(action.type){
      case "everythingDispatched" :
           everythingDispatched: true
           break;
    }
    
    然后在组件中像这样使用MapStateTrops

    ajax = async () => {
      try{
        const { dispatch } = this.props;
        dispatch(loadPack('ars'));
        dispatch(loadPack('acr'));
        await dispatch(loadProds());
        await dispatch(loadRepls());
        await dispatch(checkEligibile());
        // Add one more action here
        await dispatch(everythingDispatched());
        }catch (e) { console.log(e)}
     }
    
    render() {
     const { everythingDispatched } = this.props;
     return everythingDispatched ? <Component/> : null;
    }
    
    function mapStateToProps(state){
      return {
           everythingDispatched:state.dispatchTrackerReducer.everythingDispatche
      }
    }
    
    render(){
    const{everythingDispatched}=this.props;
    是否返回everythingDispatched?:空;
    }
    函数MapStateTops(状态){
    返回{
    everythingDispatched:state.dispatchTrackerReducer.everythingDispatche
    }
    }
    
    因为您使用的是redux,所以您有一个全局redux状态。因此,在所有调度之后,再调度一个操作,使reducer状态变为true,这表示所有操作都已调度

    在组件中,使用dispatchStateToProps方法将减速器状态转换为道具,然后使用该道具检查是否已调度所有动作。大致应该是这样的

    ajax = async () => {
      try{
        const { dispatch } = this.props;
        dispatch(loadPack('ars'));
        dispatch(loadPack('acr'));
        await dispatch(loadProds());
        await dispatch(loadRepls());
        await dispatch(checkEligibile());
        // Add one more action here
        await dispatch(everythingDispatched());
        }catch (e) { console.log(e)}
     }
    
    render() {
     const { everythingDispatched } = this.props;
     return everythingDispatched ? <Component/> : null;
    }
    
    function mapStateToProps(state){
      return {
           everythingDispatched:state.dispatchTrackerReducer.everythingDispatche
      }
    }
    
    然后是一个保持跟踪的状态

    dispatchTrackerReducer.js
    switch(action.type){
      case "everythingDispatched" :
           everythingDispatched: true
           break;
    }
    
    然后在组件中像这样使用MapStateTrops

    ajax = async () => {
      try{
        const { dispatch } = this.props;
        dispatch(loadPack('ars'));
        dispatch(loadPack('acr'));
        await dispatch(loadProds());
        await dispatch(loadRepls());
        await dispatch(checkEligibile());
        // Add one more action here
        await dispatch(everythingDispatched());
        }catch (e) { console.log(e)}
     }
    
    render() {
     const { everythingDispatched } = this.props;
     return everythingDispatched ? <Component/> : null;
    }
    
    function mapStateToProps(state){
      return {
           everythingDispatched:state.dispatchTrackerReducer.everythingDispatche
      }
    }
    
    render(){
    const{everythingDispatched}=this.props;
    是否返回everythingDispatched?:空;
    }
    函数MapStateTops(状态){
    返回{
    everythingDispatched:state.dispatchTrackerReducer.everythingDispatche
    }
    }
    
    尝试
    componentDidMount
    替代。尝试
    componentDidMount
    替代。谢谢,这很有效!我想知道我是否应该从componentWillMount中提取所有内容,并将其他函数放入everythingDispatched中。这样,该组件只需分派所有分派的内容,而不必分派大量分派。但是,我认为将所有这些异步函数添加到everythingDispatched将不再是一个纯函数,而是一种反redux模式。你知道如何重构分派分组,使这个组件只分派所有分派的内容吗?这两种方法都很好。尽管我通常更喜欢像您现在所做的那样,在组件中单独分派所有内容。这件事很有帮助,因为只要看一下组件文件,我就知道这个组件要分派什么,这有助于我理解组件。如果你将它们组合起来,只分派一个,那么你会将它们放在不同的文件中。因此,每当您想知道这个组件正在分派什么时,您都必须跳转到不同的文件。然而,这只是我的偏好。你做最适合你的事。谢谢,这很有效!我想知道我是否应该从componentWillMount中提取所有内容,并将其他函数放入everythingDispatched中。这样,该组件只需分派所有分派的内容,而不必分派大量分派。但是,我认为将所有这些异步函数添加到everythingDispatched将不再是一个纯函数,而是一种反redux模式。你知道如何重构分派分组,使这个组件只分派所有分派的内容吗?这两种方法都很好。尽管我通常更喜欢像您现在所做的那样,在组件中单独分派所有内容。这件事很有帮助,因为只要看一下组件文件,我就知道这个组件要分派什么,这有助于我理解组件。如果你将它们组合起来,只分派一个,那么你会将它们放在不同的文件中。因此,每当您想知道这个组件正在分派什么时,您都必须跳转到不同的文件。然而,这只是我的偏好。你做最适合你的事。