Redux Thunk Action Creator 未被调用

Redux Thunk Action Creator Not Getting Called

我在 actions/index 中有一个函数调用一个动作创建器,该创建器分派一个动作但它从未被调用。我想我在 Redux 中遗漏了一些东西。数据库是一个 Firebase 实例。

export function handleInitialVisit() {
  ...
  DB.child('profiles')
    .child(id)
    .set(data)
    .then(res => handleVisit());
}

function handleVisit() {
  console.log('called') // called
  return (dispatch, getState) => {
    console.log('called'); // never called
    ...
    dispatch({type: SET_DB_REF, payload: visitReferencePath});
  }
}

我唯一的猜测是,因为我没有使用 connect 或 store 调用它,所以它没有 dispatch 和 getState 的那些属性。问题是,我不需要从组件调用它。我希望在渲染组件之前发生这种情况。它试图在交互发生和组件呈现之前找出用户。

您应该使用 dispatch 函数来调度操作:

export function handleInitialVisit() {
  ...
  DB.child('profiles')
    .child(id)
    .set(data)
    .then(res => dispatch(handleVisit()));
}