等待在组件外完成派遣操作

问题描述 投票:1回答:1

我有一个屏幕,用户可以从中选择一种测验,然后应生成测验问题,应在商店中更新currentGameInformation,然后应该可以看到新的屏幕。

由于调度动作是异步的,所以有时currentGameInformation不会更新,因此我的应用程序进入下一页时会崩溃。我希望它一直等到下一页,以便提供信息。

按下按钮时,在名为startTheGame()的组件中调用了一个函数>

//inside the screen component 
startTheGame = async (id) => {
    let navigation = this.props.navigation;
    await StartTheGame(MASTER_TIME_PERIOD, {time_period_id: id}).then(function(){
        console.log("Navigating");
        navigation.replace('Quiz');
    });

};


//This function is located outside the component,
//It is a library that handles all the Quiz functionalities
export async function StartTheGame(type, details) {
let state = store.getState();
let username = state.currentUser.username;
if(username === undefined){
   //AWS gets the current user working fine and waiting to be completed
   let user = await GetCurrentUserAWS(); 
   username = user.username;
}
//set game status to loading
let currentGameInfo = {};

let currentDayPoints = await GetCurrentDayPointsForUserDB(username); 

//Redux Thunk function (is sent, but not waiting to get done) 
SetCurrentDayPoints(currentDayPoints); 
//redux thunk function (is set but not waiting for it to be done) 
SetGameStatus(SET_GAME_START_LOADING, QUIZ_GAME_START_STATUS_LOADING); 
//at this point, current day points are either updated/not and same with game status

let questions = await GenerateQuestions(type, details).catch(err => {
    SetGameStatus(SET_GAME_START_ERROR, QUIZ_GAME_START_STATUS_ERROR); //same not waiting to be completed
});

currentGameInfo = {
    questions: questions,
    points: 0,
    questionIndexesAnsweredCorrectly: [],
    questionIndexesAnsweredIncorrectly: [],
    shouldRestartBeEnabled: false,
    currIndex:0,
    questionsAnsweredInRow:0,
    gameType:type
};
SetGameStatusSuccess(currentGameInfo); //same not waiting 
return currentGameInfo; }

我的目标是仅在SetGameStatusSuccess已完成的情况下才返回

export function SetGameStatusSuccess(currentGameInfo){
return (dispatch, getState) => {
    dispatch({type: SET_GAME_START_SUCCESS, payload:{
            gameStatus:QUIZ_GAME_START_STATUS_STARTED,
            currentGameInformation:currentGameInfo
    }});
}; }

我想知道是否有一种方法不需要mapDispatchToProps函数?

我有一个屏幕,用户可以从中选择一种测验,然后应生成测验问题,应在商店中更新currentGameInformation,然后应该可以看到新的屏幕。由于...

reactjs react-native redux react-redux redux-thunk
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.