redux 和 redux-saga : Reducer returns 一个 promise 而不是常规的 object

redux and redux-saga : Reducer returns a promise instead of a regular object

正如标题所说,我的 reducer return 是一个 promise 而不是常规的 object。我假设这是因为我正在使用 redux-saga 进行异步请求,这有点不合理。有没有办法让我的 reducer return 变成解析的 object?

// reducer.tsx
...
const initialState = {
  token: '',
  error: null,
  loading: false
};

const authenticationReducer = async (state = initialState, action) => {
  switch (action.type) {
    case EMAIL_LOGIN: {
      console.log('action.payload: ', action.payload);
      const {token} = action.payload;

      return {
        ...state,
        token
      };
    }

...
// saga.tsx

function* emailLogin({type, payload}) {
  try {
    const {email, password} = payload;

    const loginUserInfo = {
      email,
      password,
    };

    const response = yield call(axios, `${USER_ADDRESS}/login/`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: loginUserInfo,
    });

    yield put(setLoginStatus(response));
  } catch (e) {}
}

但出于某种原因,useSelector()useStore() 都表明我的减速器 return 是一个承诺

//  Login.tsx

...
const token = useSelector(state =>{ console.log(state)}) // {authentication: Promise}
const state = useStore().getState();
console.log(state)   // {authenitcation: Promise}
...

请帮忙!

async 总是 return 一个承诺。 Redux 减速器必须永远 是异步的!

在这种情况下,您需要做的就是从 reducer 中删除 async 关键字。