Redux 异步操作 getState() 返回未定义?
Redux async actions getState() returning undefined?
我遇到了 redux-thunk 遇到的问题,当我第一次调用 getState() 时,我正在尝试获取我的 redux 状态的属性以便调度相关操作,我得到了值,但随后调用 getState() returns 未定义,我注意到当我检查状态时,订阅数组在第一次调用后为空,我不确定为什么会这样。下面是我的代码片段......在此先感谢大家。
因为你在使用pop()
The pop() method removes the last element from an array and returns
that element. This method changes the length of the array.
所以你在做 getState().user.subscriptions.pop()
时实际上是在改变你的状态
如果您想要数组的最后一项(不删除),请执行以下操作:
const subscriptions = getState().user.subscriptions;
let currentPlan = subscriptions[subscriptions.length - 1];
我遇到了 redux-thunk 遇到的问题,当我第一次调用 getState() 时,我正在尝试获取我的 redux 状态的属性以便调度相关操作,我得到了值,但随后调用 getState() returns 未定义,我注意到当我检查状态时,订阅数组在第一次调用后为空,我不确定为什么会这样。下面是我的代码片段......在此先感谢大家。
因为你在使用pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
所以你在做 getState().user.subscriptions.pop()
如果您想要数组的最后一项(不删除),请执行以下操作:
const subscriptions = getState().user.subscriptions;
let currentPlan = subscriptions[subscriptions.length - 1];