Working through this coding exercise and having trouble understanding state changes with Redux…the "RecountVotes" case should activate and re-render the Header component (displaying total votes) every time the RecountVotes function is called but it lags behind one step, displaying the previous collections votes as opposed to the currents…
import { PostsError, PostsLoaded, RecountVotes } from '../actions/posts';
const InitialState = {
error: null,
pages: 0,
posts: [],
votes: 0,
};
export default function posts(state = InitialState, action) {
switch (action.type) {
case PostsError: {
return {
...state,
error: action.error,
};
}
case PostsLoaded: {
return {
...state,
error: null,
pages: action.pages,
posts: action.posts,
};
}
case RecountVotes: {
let votes = state.posts.reduce((prev, post) => {
return prev + post.votes;
}, 0);
return {...state, votes}
};
default:
return state;
}
}
i’ve tried turning the header into a "PureComponent" but that didn’t seem to do much, and it’s driving me crazy!
2
Answers
Redux is not async, hence when you dispatch an action to update the state, the state change does not happen immediately. That’s why when you are trying to access the updated state right after dispatching the action you are getting the old state.
you can use redux-thunk to solve this, can refer to their official documentation. I can provide you with a samplecode(u can customize according to you)
in your Reduce add below
i hope this would work properly. Also kindly try to give more context of where you are using these by editing this post so i can help you more efficiently
Solutions
State Update Timing or Lag:
Use Redux DevTools to inspect the state changes. Verify that the state is updated immediately after the RecountVotes action is dispatched. You can also add a console.log inside the reducer to see the votes calculation:
State Reference and Immutable Updates:
• Solution: Ensure that your state changes result in new references so Redux can detect the change: