Redux-Saga 不等待使用 redux-saga-firebase 完成操作

时间:2020-12-23 16:38:13

标签: reactjs firebase redux redux-saga

我正在使用 redux-saga-firebase 包来执行一些 firebase/auth 操作,但问题是在调度完成之前执行了调度之后的代码。 例如:

dispatch({ type: 'SIGNIN_WITH_EMAIL_AND_PASSWORD_REQUESTED', email: 'test@test.com', password: 'qwerty123@'})
console.log(`propsUser: ${this.props.user.currentUser}`);

Saga.js

function* signInWithEmailAndPassword(data) {
  try {
   const user = yield call(reduxSagaFirebase.auth.signInWithEmailAndPassword, data.email, data.password);
   yield put({ type: SIGNIN_WITH_EMAIL_AND_PASSWORD_SUCCEEDED, currentUser: user });
  }
  catch (error) {
    yield put({ type: SIGNIN_WITH_EMAIL_AND_PASSWORD_FAILED, message: error.message });
  }
}


export default function* userRootSaga() {
  //yield fork(syncUserSaga)
  yield all([
    takeLatest(SIGNIN_WITH_EMAIL_AND_PASSWORD_REQUESTED, signInWithEmailAndPassword),
  ])
}

减速器

case SIGNIN_WITH_EMAIL_AND_PASSWORD_SUCCEEDED:
     console.log(`action: ${Object.values(action)}`); //here I get the correct user the second time
     return { ...state, currentUser: action.currentUser }

在那种情况下,当我第一次按下按钮时 this.props.user.currentUser 是未定义的,但是如果我在两三秒后再次按下它,我会得到用户对象。 如何在调用减速器之前等待操作完成?

1 个答案:

答案 0 :(得分:0)

case SIGNIN_WITH_EMAIL_AND_PASSWORD_REQUESTED:
     return { ...state, isLoading: true, isLoaded: false }

case SIGNIN_WITH_EMAIL_AND_PASSWORD_REQUESTED:
     return { ...state, isLoading: false, isLoaded: true, currentUser: action.currentUser }

然后您需要呈现登录按钮,如:

<LoginButton disabled={isLoading}>
相关问题