score:2
Accepted answer
Option 3 and opntion 4 are incorrect, the prevState/myState is an array and you are returning an object, this will surely cause error in typescript. As to option 1 and 2, they only differ between declaritive and imperitive way of programming. And declaritive programming makes more readable code.
On option 1:
const newState = [...myState];
newState[index].description = "new desc";
// Access index position as opposed to id
// since the item's position may not align with it's id
setState(newState );
On option 2 with map
function, it can be written as follows:
setState(myState.map(item => item.id === id ? {...item, item.description: "new desc"} : item))
In conclusion, use map/filter is more preferable to update array.
Here's sandbox to compare option 1 and 2
Source: stackoverflow.com
Related Query
- React updating single object of an array of objects - map vs entire array
- updating a single object key value inside array of objects react redux state
- Problem with updating object property in array of objects in React
- React map object and embedded object from JSON and convert to single array for state
- How to map an array of objects in React
- Updating the array object in React state using immutability helper
- ReactJS updating a single object inside a state array
- Get first object from array of objects in react
- react setState array of object using map doesn't work?
- React not updating state with setState with Array of object
- Updating property of object within stored array - React Native
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- Updating the values in an array of objects in react js
- REACT JS: Map over an array of objects to render in JSX
- Remove element of an object from array of objects - react state
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- React - convert object into array of objects with properties
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- How to map through nested array of objects in React
- React how to conditionally edit or add object in array of objects using setState
- React useReducer not updating object in array
- Error: Objects are not valid as a React child (found: object with keys {}). use an array instead
- React map object containing array
- Updating only one property inside an array of objects react
- React Redux: Unable to Map array of Objects
- updating a property in an array of object immutably using react hooks
- Objects are not valid as a React child (found: object with keys {job}). If you meant to render a collection of children, use an array instead
- React - unique key in object gets overwritten when updating state array
- Mirroring the position when moving objects from the array to the array to another object in React
- How to map over 2 Array of object in React and render to Table
More Query from same tag
- JavaScript, How to concat objects into arrays in LocalStorage?
- create-react-app install devDepencies in dependencies section
- "Error: You may not call store.getState() while the reducer is executing."
- How to make Snackbar open again?
- React JS: Component not updating, after calling setState
- How to redirect ReactJS page to plain HTML page using button click?
- I am getting 404 not found in my fetch post request
- How do I write a unit test to call a function inside useEffect?
- How to animate a div with a position fixed in react js?
- how to send function in typescript interface?
- React setting Stateful variables in map function
- React Native - Redux state is updating but component view isn't updating properly
- Copy to clipboard using ReactJS
- How to clean up this `await nextEvent(element, 'mousemove')` pattern when I no longer need it?
- Wrapping UI components to use with Redux Form, and use custom onChange event functions
- Why isn't my first click being registered in state in my React container?
- Can I use React.lazy inside a function?
- validation for radio buttons ( at least one is checked )
- Can I set PDF resolution (pixels per inch) in react-pdf?
- is there a way to pass a function to the "updater" function that was provide by the `useState()`'s React Hooks?
- Using StorybookJS in a component library package with React as peer dependency
- react functional component not get initialised
- Modal Not Appearing onClick
- Append a button at the end of Autocomplete options in Material-UI
- React Native - Return all JSON data in AsyncStorage?
- ReactJS: State only updates every second click
- Passing data from UseEffect to Chart.js not working
- How to set background image using react refs?
- How can i export Multiple With Routers
- How to replace a word in a sentence based on a condition in ReactJS