score:0
Accepted answer
you aren't using the map function correctly. array::map
returns a new array, the values of the original array mapped to new values.
update handlefeeandinch
simply be the map callback, returning a new value into a new array, then set state with that array.
handlefeetandinch = (key, namevalue, value) => {
let realfeet = (value * 0.3937) / 12;
let feet = math.floor(realfeet);
let inches = math.round((realfeet - feet) * 12);
let updatedvalue = `${feet}'${inches}"`;
// return new measurement value
return {
name: namevalue,
value: updatedvalue
};
};
handleunitchange = () => {
if (this.state.iscm) {
// reset state
} else {
// get array of new measurements
const newmeasurements = this.state.measurements.map((measurement, key) =>
measurement.algo === 1
? this.handlefeetandinch(key, measurement.name, measurement.value)
: this.handleinches(key, measurement.name, measurement.value)
);
this.setstate(prevstate => ({
measurements: newmeasurements
}));
this.setstate(prevstate => ({
iscm: !prevstate.iscm
}));
}
};
Source: stackoverflow.com
Related Query
- how do i update state in react for updating the value of an object inside an array using array.map function
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to update the state of nested json object in react using hooks
- How to update specific value when the state is an array of objects - React
- Updating state for a value inside an object immutably in redux reducers
- how to print the elements value inside an object react using es6
- What is the right way to toggle a value inside object state in React hooks?
- How do I update the state by updating an array within a particular object from an array of objects?
- How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js
- How to define TypeScript types when used with React useState and the previous state inside a state updating function?
- React - How to use the current state value as a variable so i can set as a index inside an array
- How to update the value of a prop upon a state change in React
- Using React hooks, how to update the state of part of an object in an array of objects
- updating a single object key value inside array of objects react redux state
- How to update react state with dropdown menu selection? I am using semantic UI for the dropdown
- How to prepend a value to an array inside the object of state in Reactjs
- Add a new value inside the State object in React Hook
- how to update object data of the state in react
- how can I set the reducer's return value to state object in component in react js
- How to type the initial state for react context when using useState as value
- is it possible to give an empty object in react state for the init default value
- How to instantly update react state after replacing the value from array
- How to update a single object inside the array with useReduce on state change
- 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
- React onSubmit wait for async IIFE inside the function to update state with hooks
- How to update react redux state Object of id - key value pair
- How can I update react state quick enough in order for the hover effect to feel responsive?
- Rewrite a function to find an object by a matching id inside an array, update a value and set a react state
- How to increment the value of an object within state React
More Query from same tag
- React-cookie: Invariant Violation: The root route must render a single element
- Regarding refs in react JS / Can I use a ref to set component's state?
- "Warning: Can't call setState (or forceUpdate) on an unmounted component", but component does not have setState?
- Unable to get Hot Module Replacement working on different port
- Put operations out of useEffect?
- Reset a React Native Formik form from outside the form
- How to include local script file on React page?
- change in state does not re-renders the component
- how to toggle all checkbox by clicking header checkbox react js
- Include some node_modules directories in Babel 7
- Sorted Objected based on nested object value
- How to link two components with html/components inbetween with React.js
- Why is there a difference in style between the 2 divs?
- Hooks setState always one step behind
- Webpack config for Code splitting not working for production build
- Want to give different state to each element with redux
- Next JS - Handling getInitialProps on _app.js in SSR vs CSR
- React HOC returns an anonymous _class
- Trying to refactor React App to React-Redux issue w/ volume slider
- Extracting and referencing multiple CSS files in ejected create-react-app
- I am receiving an error when I run NPM start
- React runs an infinite loop when updating real time database
- React hook state not change in recursion
- Unhandled Rejection (TypeError): Cannot read property '0' of undefined
- useState with condition inside a map
- Append objects in array using React.js
- Untangling state changes in onMouseEnter, onMouseLeave, and onClick
- split material ui style to other file
- Vite Reload Loop
- how to get value onchange event when state data in object