score:10

Accepted answer

in redux, you should avoid directly mutating the state of your reducer. refrain from doing something like state.reducers = blah. in order for redux to know that you are trying to make an update to state, you need to return a completely new state object. following these principles, your reducers will update correctly and your components will get the new data.

reducer.js

const initialstate = {
    educations: []
};

export default function home(state = initialstate, action){
    switch(action.type){
        case get_educations: {
            return {
               ...state,
               educations: action.payload
            };
        }
        default:
            return state;
    }
}

in the code above, we return a new state object. it will include everything from the existing state, hence ...state, and we just update the educations property with the action.payload.

score:0

it looks like you’re mutating the state in the reducer. the reducer should always return a new state object if something updated.

you could do what the answers above suggest, but i would recommend using a package like immer (https://www.npmjs.com/package/immer) or immutable.js to prevent any bugs down the line. using the spread syntax can be dangerous if your state object has some deeply nested properties, and it’s hard to be 100% sure that you haven’t accidentally mutated something, especially as your app grows in size.

score:0

it looks like you have solved this while i was getting this typed up - i decided to post it regardless, as it may be helpful.

on top of what christopher ngo already mentioned, the following example outlines how you can interact with your store to create new educations and then view them, in separate components..

edit modest-fermat-98s0r

cheers!

score:0

i encounter this all the time and resolved it with clear then get/set state. this ensures a reset of the state call.

reducers.js

const initialstate = {
    educations: []
};

export default function home(state = initialstate, action){
    switch(action.type){
        case get_educations: {
            return {
               ...state,
               educations: action.payload
            };
        }
        case clear_educations: {
            return initialstate;
        }
        default:
            return state;
    }
}

hooks.js

...
const cleareducation = () => {
  dispatch({ type: clear_education });
}
   const geteducations = (payload) => {
      cleareducation(); // this clearing of the state is key fire re-render
      dispatch({ type: get_educations, payload });
   };

}

score:1

can try with the reducer written this way :

const initialstate = {
        educations: []
    };

export default function home(state = initialstate, action){
    switch(action.type){
        case get_educations: 
        return {
            ...state, educations:action.payload
        }

        default:
            return state;
    }
}

Related Query

More Query from same tag