score:10
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..
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;
}
}
Source: stackoverflow.com
Related Query
- Props not updating when redux state change in React Hooks
- React shouldComponentUpdate() is called even when props or state for that component did not change
- Component not updating when I change the props that I pass to it in React
- React is not updating when Redux state is changed below the first level
- REACT + REDUX: on redux state change mapStateToProps updating state but view is not rendering as per new state
- react / redux toolkit useSelector not updating when store state changed
- React child component not updating when props change
- React hooks state not updating when mapping
- React Component does not re rendering when updating an object in state hooks
- State does not change via redux when default props is assigned
- React component not updating when redux state is updating
- Props not updating when I change the store in react
- React Redux state not updating when using non mutable methods
- Updating state on props change in React Form
- State not updating when using React state hook within setInterval
- React Child Component Not Updating After Parent State Change
- React Hooks - useEffect fires even though the state did not change
- React component not updating when store state has changed
- State not updating when receiving new props (ReactJS)
- react redux useSelector rerender even when data does not change
- React leaflet center attribute does not change when the center state changes
- react props not updating with redux store
- React hooks state variable not updating after rerender
- react is not updating functional component state on input change
- React hooks - state in useState() is not reset when route is changed
- how and when to call a react component methods after state change from redux
- Redux not updating components when deep Immutable state properties are updated
- React Child with useEffect() not updating on Parent State Change
- Component not updating on Map (dictionary) state change using hooks
- Component does not rerender but redux state has changed via react hooks
More Query from same tag
- JSON issue seems like it should work
- Installing Gatsby plugins breaks entire app
- AWS Amplify hosted React App url redirects is tricky
- Request header field <field-name> is not allowed by Access-Control-Allow-Headers in preflight response
- Is it possible to implement an inheritance like mechanism for React components in order to reduce nearly duplicate code?
- Problem showing global spinner using redux and react
- React + Firebase Cloud Functions in Typescript fails to deploy
- How to have multiple react-select options
- How to add listener on change Object which already defined with @observable decorator in MobX to use in React
- I want to choose one specific option when clicked on button and button should got disabled in react
- .babelrc being hidden in in MacOS Mojave update causing build fail
- Material-UI Drawer with Flexbox horizontal scroll not working on iOS Safari
- How to deploy with sequelize and react in heroku
- React material table automatic page size
- Issues when Hosting express with typescript and react app on Heroku
- Overriding global type in typescript
- How to pass props into an imported function component?
- Show loading spinner
- Local state in container component
- call my function from API to front using react and node
- The ultimate search with query strings
- Assign local storage value in React
- How to limit the range of an element?
- React.js: How can I render a single component from an array?
- How do you repeat a character, after content, and fill it to 100% of width? JSX / React
- Problem installing packages with yarn on Ubuntu 18.04
- How to store multiple set of data in local storage in reactjs?
- Getting an arrow function syntax error in React
- React - useRef with TypeScript and functional component
- React render array