score:4
Accepted answer
the problem you are facing is that you are mutating the state object, which means that at the end, prevstate === nextstate
and react bails out of rendering. an option is to use a new object and copy the props like this, using the same combo of object.keys
and foreach
but adding an extra step:
setstate(prevstate => {
const nextstate = {}
object.keys(prevstate).foreach(key => {
nextstate[key] = true
})
return nextstate
})
Source: stackoverflow.com
Related Query
- React useState - update all object values to true
- How can i update values for all keys in nested object at once in react js?
- useState to update multiple values in React
- React efficiently update object in array with useState hook
- React - How to loop and update values in object model?
- React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
- How to update a json object request string values to NUMBER in REACT
- React JS: set all values of nested object to false except for one
- React Hooks, useState related question. How to update one property of object and keep the rest
- Update all Object Elements of a State Array with React Hooks
- Better way to empty all the values of an object by setState in react
- React - how to update a specific field in Object Array using useState in UseEffect
- Update array of object in React useState
- React - UseState - How to partially update state object
- How to update 0th index of array of strings present in react state keeping all other index values unaffected
- update an object with useState hook in React
- Convert React useState hooks to pass in props and update prop values
- Updating multiple array object values with useState in React
- React useState with object update several states in object
- When useState update then map loop is not working in array inside of object in useState. React js
- React useState hook - update state of a object
- How to update an object all elements inside map function in react
- How to sort sum up all values from object arrays inside react props
- How to keep the updated state array of object values when update the same state again in React JS useState?
- How to update value of a key in object in an array with the new value in React useState in TypeScript
- Taking the initial values of an object using useState hook in React
- React update or add to an array of objects in useState when a new object is received
- React js, how to update state object values on input change?
- React - update object using useState hook
- React Hooks: Set All State Values to the Same Values with useState
More Query from same tag
- location.state fail with gatsby build
- Use MobX with React Context while using Mobx Persist Store?
- How to combine multiple inline style objects?
- Using React and react-slick, I am just having a general hard time understanding how to use it
- Render card details on another page when clicking specific card
- ReactJS how to use if function
- Fire event listener on element not rendered in React
- How to import React component as embedded svg image
- Setting onSubmit in React.js
- How to achieve a progress circle that fills color with the increase in percentage in reactjs?
- Passing state between nested components
- How to make Material-UI Snackbar not take up the whole screen width using anchorOrigin?
- Creating a Custom Styled Prop Using Styled-System
- How to avoid re-renders in React when using conditional Render and children node prop
- How to Set Style to image pattern on mapbox GL
- How to convert html to JSX, ReactJS Component?
- Why does listing a variable as a dependency "solve" the stale closure problem?
- TypeScript: Why is Parameters generic accepting incorrect arguments?
- How to add element with props in react router V6
- Access vuforia vws with react forbidden header
- How to update object element in array in React State
- ReactJS (beginner) Using Youtube API - Returned Object Not Allowing Me to Access VideoId Property
- how to open non default browsers when doing npm start for a react project
- ReactJS Too many re-renders. React limits the number of renders to prevent an infinite loop
- Material-UI Grid does not hide when using display
- How to instantiate react component with injected properties
- I want to SetState the items of a child component from the child component to the parent component
- this.props.function is not a function - React js
- Warning: Encountered two children with the same key for react infinite scroll
- styled-jsx dynamic styles not applied