samtoya
samtoya

Reputation: 79

Redux async actions getState() returning undefined?

I'm stuck with a problem I encountered with redux-thunk, I'm trying to get the properties of my redux state so as to dispatch relevant actions, when I call the getState() the first time, I get the values but subsequent calls to getState() returns undefined, I noticed when i checked the state, the subscriptions array was empty after the first call, i'm not sure why this is happening. Below is a snippet of my code... Thanks in advance guys.

enter image description here

enter image description here

enter image description here

Upvotes: 0

Views: 494

Answers (1)

muZk
muZk

Reputation: 3038

It is because you are using pop()

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

So you're actually changing your state when doing getState().user.subscriptions.pop()

If you want the last item of the array (without removing) do this:

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

Upvotes: 1

Related Questions