React + Redux : When using a redux reducer, Is the use of prevstate necessary if we want to rely on previous state? Code Answer

Using Redux, if we want to rely on previous state to change the value of current state, don’t we need to use the prevstate callback function like we do when we call a regular setState setstate(prevstate => !prevstate.boolean )? (if we wanted to change the value a boolean inside the state depending on previous value of boolean for exemple).

So is the exemple below OK ? there won’t be any batched setState or something that would cause a “twitchy” state ?

const initialState = {
  showToolbar: false,

const toolbarReducer = (state = initialState, action) => {
  switch (action.type) {
    case "TOGGLE_TOOLBAR":
      return {
        showToolbar: !state.showToolbar,
      return state;

export default toolbarReducer;

I hope my question makes sense !

Thank you !



In Redux reducers you don’t need to worry about a callback to ensure you’re working with the latest state because there’s no behind the scenes batching of the updates.

Each Redux action/state update is executed when it’s called.

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji