score:2
Accepted answer
Instead of destructuring the array and using map, I typically update a single item's value with a single map that replaces the item I am updating and returns the existing value for all other items. Something like this:
this.setState((prevState) => {
return {
todoList: prevState.todoList.map((task) => {
if (task.id === key) {
return { ...task, completed: true };
} else {
return task;
}
}),
};
});
Also, notice that this example passes a function to this.setState
rather than an object. If you are updating the state based on the previous state (in this example using todoList
from the previous state) you should use the function method. setState
is asynchronous and you could get unexpected results from using this.state
to compute the new state.
Source: stackoverflow.com
Related Query
- How to update certain object in state array based on received key from DOM
- How can i update a name field from single element in array of object from a state in reducer in React-Redux?
- How do I update the state by updating an array within a particular object from an array of objects?
- How can I update the state of an object nested in an array from a child component?
- How to update certain key values in an object nested in the redux state
- how to update array of object in react state based on id
- How can i get array of object from this data for state and count in reactjs
- Redux - Find object in state array by its key and update its another key
- React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
- How can I show certain elements from an array based on a React State?
- How to update Object in state from e.target.name
- How delete an object based on its index from a nested array in React?
- How do i update value of a key in an object inside an Array in React?
- How to update object properties in state array
- How to update array within state object with react/redux
- How do I update an array within an object from within state?
- how do i update state in react for updating the value of an object inside an array using array.map function
- How to update state object key value?
- How to remove Object from Array based on ID in react?
- React-Redux update state of immutable object with nested array, delete element from array
- How to get value based on key from an array of objects javascript
- Update property of certain object inside array in state
- how to filter based on key from array using javascript?
- Update state with object from key input [React]
- How to check and return value of state object array and use that to identify which object to take data from
- How to update a key indexed array state in useReducer?
- How do I make an axios request using a route parameter from React Router, then update state based on the response?
- How do I make an array from a JSON object that matches a certain key?
- How to delete an object from state when it is inside an array of objects
- Using React hooks, how to update the state of part of an object in an array of objects
More Query from same tag
- Prevent adding backslash to \n special character in a string
- when i run npm install react-currency format . I'm getting the below error? what can i do?
- Check screen size change for a specific width
- React update state in parent from child components
- ReactJS - Warning: A component is changing an uncontrolled input of type text to be controlled
- ReactJS with useRef: focusing a toggle-able form
- Unhandled Rejection (AbortError): The operation was aborted
- react-redux get width of component's parent div
- next/image loads very big images
- React redux render data
- React Hook is called in function that is neither a React function component nor a custom React Hook function
- How to obtain ES6 module of React and ReactDOM?
- How can I await for state to be set if it doesn't return a promise?
- Checkbox toggle in react
- How to iterate Multiple states in a single table
- React's invalid prop type 'array' expected 'object'
- to many re-renders error react, how to avoid it
- how to send props from a Component to Redux FieldArray
- Redux reducer initial state with Typescript
- Material UI Button contained style not working with Transition
- React Multiselect checkbox filtering data
- react-player uploading and playing new video
- How to get Accounts.onLogin to impact my app in Meteor React ES6?
- retrieving data from firestore using uid react
- Mock state in react snapshot test?
- How to wait for an API response for maximum 15 seconds in react
- Error when compiling with webpack and babel-loader
- Color opacity on an image background
- React JS Parent Child Component Events Sharing
- How can I dynamically split chunk of text into pages for jsPDF?