Computed value updating one state change behind action
See original GitHub issueCurrently, I have an array of strings called currentCycles, an Action used to set this array called setCurrentCycles, and a computed value that should update every time the setCurrentCycles action is called. The computed value uses the currentCycles value to retrieve the correct data. However, the value of currentCycles always seems to be one update behind the value it should be set to after called setCurrentCycles.
I am using redux-persist, typescript, as well as easy-peasy version 3.0.2 if that helps with the problem as well. It’s worth noting that if I make a change to the store state later on, the computed value gets re-computed and the value of filteredTokens is correct. Also, if I do a direct request to currentCycles through useStoreState, the values are correct. Here is the code I’m using as well as some console logs that hopefully help with the debugging process:
currentCycles: [],
setCurrentCycles: action((state, payload) => {
// Action: should print out an array of cycleIDs, which are strings
console.log('Action: ', JSON.stringify(payload));
state.currentCycles = payload;
}),
filteredTokens: computed(
[state => state.currentCycles, state => state.token.tokens, state => state.workflow.currentWorkflowID],
(currentCycles, tokens, currentWorkflowId) => {
// Computed: Should print out the same cycleIDs from the action, but rather are printing the cycleID from a previous update
console.log('Computed: ', JSON.stringify(currentCycles));
return tokens.filter(token => token.workflowId === currentWorkflowId && currentCycles.includes(token.cycleId));
}
)
Current Values: Start:
Computed: ["ihoisdg293jpinvzlknalksf","newcycle"]
Remove ID “ihoisdg293jpinvzlknalksf”:
Action: ["newcycle"]
Computed: ["ihoisdg293jpinvzlknalksf","newcycle"]
Remove ID “newcycle”:
Action: []
Computed: ["newcycle"]
Expected Values: Start:
Computed: ["ihoisdg293jpinvzlknalksf","newcycle"]
Remove ID “ihoisdg293jpinvzlknalksf”:
Action: ["newcycle"]
Computed: ["newcycle"]
Remove ID “newcycle”:
Action: []
Computed: []
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)

Top Related StackOverflow Question
Sounds great on the timeline! I sent a little ‘coffee’ money your way. The amount of trouble
easy-peasyhas saved me was easily worth it. I hope it helps! Thanks @ctrlplusb!Hey, it’s great to hear that’s in progress already! I’ll make a new branch and give it a shot a little later today. I’ll post the results as soon as I have them. Thanks for your hard work on the library! I can’t recommend it to enough people!