In the state.unsortedAllHighScores there will be an array output, but when I try to get that state and sort that in state.sortedAllHighScores, it stays empty? My expected result would be an array that should be sorted after every dispatch, so when the dispatched is being called, it should output the sorted array. This is a highscore ranking, so you know why I want to have that sort function in here.
const initialState = {
sortedAllHighScores: [],
unsortedAllHighScores: []
};
export const game = (state = initialState, action) => {
switch(action.type) {
case types.HIGH_SCORE: {
return {
...state,
unsortedAllHighScores: [...state.unsortedAllHighScores, state.updatedCurrentScore],
sortedAllHighScores: [].concat(state.unsortedAllHighScores).sort((a, b) => {
console.log(b - a);
return b - a;
})
}
}
}
[...state.unsortedAllHighScores.sort()]state.updateCurrentScoreto your initial state to avoid confusion on how that's being set. 3. I'd break up the assignment tounsortedAllHighScoresfrom theHIGH_SCOREaction type, maybe something likeSET_SCORES, because you have to update the state ofunsortedAllHighScoresbefore you can then use it in thesortedAllHighScoresstate as a sorted value. In short, you're doing way too many things at once and it's yielding confusing bugs. Atomization is your friend.