score:1
Accepted answer
One of the core principles of react is that state is immutable. After you set state, react is going to do a ===
between the old state and the new state. If that results in true, then react believes that nothing has changed and skips rendering. Since you are mutating the state, it will pass an ===
, and react cannot tell that you mutated the object.
So instead of mutating the state object, you will need to create a new one:
setTest(old => {
return {
...old,
name: "new name"
}
})
score:0
try
function test(){
setTest({...testing,name : "new name"})
}
Source: stackoverflow.com
Related Query
- React reload on setState of an array
- SetState of an array of Objects in React
- setState not working for updating an array in React
- react setState array of object using map doesn't work?
- React Hooks setState of an element in an array
- React not updating state with setState with Array of object
- React setState of array of objects
- React input setState for array of objects
- React useState, setState in useEffect not updating array
- React convert props Objects into array then setState
- react setstate not working when pushing elements to array
- setState update array value using index in react
- React how to conditionally edit or add object in array of objects using setState
- React setState array is appending the same item twice on second call onward
- React setState not updating reduced array
- React - state contains array of objects. setState with conditional
- how to update specific index of integer array in functional component setState in react js
- React setState overwriting an array state
- React setState array not getting sorted
- Objects are not valid as a React child when setState of an array at the end of a promise chain
- React init state array with init function that repeatedly calls setState
- Not able to setstate with respect to array of objects: React JS
- How to add to iterate array setState in React
- React array not changing with setState
- Use SetState to update one of the item in array object in react
- React setState on array of objects rerenders every component
- SetState of an Objects of array in React
- failed to concat nested array of object in setState of react
- how to change value of object in array using setState with React
- How can I add one random number to an array in React setState hook while in StrictMode
More Query from same tag
- useRef current getting its value only on second update
- Trying to loop through multiple components in React
- Selected option in onChange with react / redux
- "TypeError: this.setState(...) is undefined" when making a promise after setting state
- How to set initial value using useState hook from Redux store?
- How can I safely use setTimout in combination with refs inside useEffect?
- Meteor, react, this.props.children shows nothing
- How to improve performance when detecting multiple outside click event listener in React Hooks
- how to make component size variation with reactjs and styled components
- how can I make a function which will return a number of Schedule Dates that are matching with other Item_date from my database
- How to set PUBLIC_URL back to the server root for npm run build
- React & Deck.GL: Add default props to each child component
- React + Flux: best practice for calling an action after another action?
- Is there any way to check prop functions and pass it to the next level
- ReactJS Modal component won't close onclick
- React - dangerouslySetInnerHTML being displayed as plain text
- Change class name of list element based on incremental props value - ReactJS
- Higher Order Component - listening for onChange
- React state is not being updated by my call to the API
- TypeError: Cannot read properties of undefined (reading 'map') Trying to Read REST api and display
- "arrow function expected no return value" with clean-up function in useEffect
- How to replace useEffect hook with component lifecycle?
- NextJS process.env. variables undefined on button click fetch data
- Function passed to video.addEventListener("play") event is undefined
- I want to alert if any JSON value changes then update it in the state and then console.log
- how to display html file when clicks on list of links in react?
- How to fix the "Warning: useLayoutEffect does nothing on the server"?
- How do I add a symlink from project's node_modules?
- Needs Help To Troubleshoot Fetching Single Document From Firebase Database As Detailed Page
- react app looks different from one in w3school and other tutorials