【问题标题】:Redux - Reducer not being calledRedux - 未调用减速器
【发布时间】:2019-10-12 19:14:37
【问题描述】:

目标

我正在尝试将从 Firestore 返回的一些对象传递到我的减速器中,以便我可以将一些结果显示给用户。

问题

当我尝试调用并将查询传递给减速器时,它似乎不起作用。我正在运行一个 console.log 来查看减速器是否被调用但它没有出现。我想是因为我有嵌套的 return 语句?,这是真的吗?

这是我的行动:

export const queryBidder = (value) => {
  return async (dispatch, getState, { getFirestore }) => {
    const firestore = getFirestore();
    const normalizeValue = _.capitalize(value);
    let Query = []
    firestore.collection("bidders").where("firstname", ">=", normalizeValue).get()
    .then(snapshot => {
      if (snapshot.empty) {
        console.log('No matching bidders.');
        return;
      }  
      snapshot.forEach(doc => {
        Query.push(doc.data());
        return { type: actionTypes.QUERYBIDDER, Query: Query };
      });
   
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });
  }
};

当我将 return 语句放在 async return 语句之上时,一切正常。但是,我需要能够致电getFirestore()。这个设置来自我在 Udemy 上找到的一个教程,说实话我不太明白。

return async (dispatch, getState, { getFirestore })

这些是如何传入的?他们来自哪里?为什么顺序很重要?我怎样才能只访问getfirestore() 函数而不将我的操作逻辑包装在这个函数中?

【问题讨论】:

  • 我能够通过使用调度而不是返回来解决这个问题。仍然很想进一步了解此设置。

标签: reactjs firebase firebase-realtime-database redux


【解决方案1】:

我不确定为什么会这样,但在等待回复时,我选择从退货改为派送。

dispatch({ type: actionTypes.QUERYBIDDER, Query: Query });

这解决了我的问题。寻找更彻底的答案,了解为什么会这样以及我上面的原始问题。

【讨论】:

    【解决方案2】:

    来自有关使用调度的 redux 文档 - This is the only way to trigger a state change。所以基本上如果你想改变 redux 状态,你应该派发一个带有参数的动作 - 动作类型和新数据。

    你可以阅读更多关于 redux dispatch here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-18
      • 2018-02-02
      • 2017-03-12
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多