score:1
Accepted answer
Use prev
parameter in useState hook. The useState
hook is asynchronous.
const updateTranslatedResponse = (segmentId, key, value) => {
setTranslatedResponse( prevTranslatedResponse =>
prevTranslatedResponse.map(
el => el.segment_id == segmentId ? { ...el, [key]: value }: el
)
)
}
Source: stackoverflow.com
Related Query
- How to keep the updated state array of object values when update the same state again in React JS useState?
- React: componentDidUpdate - the previous state is the same like the current state when I update object in array
- How to filter and sort the same array of object state in redux?
- 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 specific value when the state is an array of objects - React
- Why state doesn't keep all the data in Array state in hook when it updated in foreach?
- how do i update state in react for updating the value of an object inside an array using array.map function
- How do I update the state by updating an array within a particular object from an array of objects?
- How to update state in ReactJS and use the values at the same time
- Using React hooks, how to update the state of part of an object in an array of objects
- how to update state with reducer, when my state is an array not an object
- ReactJS How to update the state in object which is in the array
- How can i get values from multiple input boxes on form submit and store the values as an object in an state array for React?
- How can I update the state of an object nested in an array from a child component?
- How to update a single object inside the array with useReduce on state change
- How to identify a certain object id of an array to change the state of an element in the same object?
- How do I update the same object format when I make a new API call in React?
- How to update certain key values in an object nested in the redux state
- How do I see state when logging to the console instead of Proxy object inside reducer action?
- useState hook can only set one object at the time and return the other object of the same array to initial state
- How to update (re-render) the child components in React when the parent's state change?
- How does React re-use child components / keep the state of child components when re-rendering the parent component?
- How to update the image when state is updated?
- How to instantly update state when any changes into the localStorage in React.js
- React State update a nested array with objects based on the id when iterated
- How do i iterate over an array of object in react.js and add the values of prices in the object as shown below
- How to display the state on the same page when clicked Submit button in react
- p5.js and React: how to update a sketch inside a component when one of the component's props is updated
- React: How to show Spinner & Component (at the same time) when State Changes using `useEffect`
- How can I pass array to object in the state in React.js
More Query from same tag
- Src folder is not created while create-react-app
- Typescript React Error
- How Do I Pass on an Array of Objects using "forEach" Function?
- React - how to optimistically set local state whilst waiting for the api call to confirm the save result?
- Change background color to gradient in react-chartjs-2
- ReactJs.Net, Gulp, Babel, Browserify MVC
- Looking for patterns to work with Timezones and dates Front-end(React) and Backend(nodejs)
- How can I change form fields based on dropdown selection with reactJS
- Why I am getting re-render errors on loading page?
- How to add custom indicator in trading view Charting library in reactjs
- React shouldComponentUpdate() does not prevent re-render
- React and TypeScript - Component<Props & {}>
- How can I simulate same behavior as history.push("/Initial"); using hash router?
- How to iterate json nested array in graphql scheme
- How to add an actual user id into react hooks
- Cannot set style-display of a div containing another component through a component in React.js
- If I press too much numbers in my Javascript calculator the numbers go outside the display
- Firebase onCreate add custom claims to the auth.user
- ReactJS - Wait third party script response before render
- NativeBase multiple icons in InputGroup
- imported action not available in container
- How to check if name already exists in Firebase Realtime Database (Firebase & JS)
- How do you use react-router-dom to redirect user when token has expired?
- React-Native + Mobx + React Navigation : Error: MobX injector: Store '...' is not available! Make sure it is provided by some Provider
- React full page image that disappears on scroll down, doesnt come back when you scroll up
- Can AnyChart the HTML version be used with React?
- How to update the router state on history.replace in react router?
- couldn't get to detail page for each restaurant from my Restaurant list
- Export TailwindCSS CSS rules to file instead of rendering CSS rules on DOM
- how to convert a functions Hook in to class state