代码之家  ›  专栏  ›  技术社区  ›  samtoya

Redux异步操作getState()返回未定义?

  •  0
  • samtoya  · 技术社区  · 8 年前

    我遇到了redux thunk遇到的一个问题,我试图获取redux状态的属性,以便分派相关操作,当我第一次调用getState()时,我得到了值,但随后对getState()的调用返回未定义的值,我注意到当我检查状态时,第一次调用后订阅数组为空,我不知道为什么会发生这种情况。下面是我的代码片段。。。提前谢谢各位。

    enter image description here

    enter image description here

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  1
  •   muZk    8 年前

    这是因为你正在使用 pop()

    方法从数组中删除最后一个元素并返回 那个元素。此方法更改数组的长度。

    所以你实际上是在改变你的状态 getState().user.subscriptions.pop()

    如果需要阵列的最后一项(不删除),请执行以下操作:

    const subscriptions = getState().user.subscriptions;
    let currentPlan = subscriptions[subscriptions.length - 1];