Props not updating when redux state change in React Hooks
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
.