score:2
Accepted answer
I believe it's better not to touch the original state of the object. Instead copying and modifying in that new object the property what we need then passing it to setState()
. The proper way is using the callback version of setting the state.
Try as the following:
setItems(prevState => ({
...prevState,
item_name: 'the new value'
}));
So the function would look like:
some_method = (new_item_name) => {
setItems(prevState => ({
...prevState,
item_name: new_item_name
}));
}
I hope this helps!
Source: stackoverflow.com
Related Query
- What is the correct way to update state and render the component?
- Getting error "Cannot update a component while rendering a different component". What is the correct way to achieve what I'm attempting?
- What is the correct way of setting state variables in Reactjs and what is the difference between these approaches?
- What is the correct way of updating state for which the value needed to be a combination of previous state and event
- What is the right way to manage synthetic events using hooks and update the state from event handlers?
- ReactJs : What is the proper way of accessing the state data in the root component where the link between React and Redux has been implemented?
- What is the correct way of adding a dependency to react in your package.json for a react component
- What is the difference between React component instance property and state property?
- TS2322 Error when extending Button, component property not found. What is the correct way to type?
- What is the correct way of dispatching an action not tied to a component with Redux?
- ReactJS: Best way to check state values and update render based on state?
- What is the 'proper' way to update a react component after an interval with hooks?
- In React, what is the right way to update nested array state items
- What is the best and most efficient way to bind callbacks in ReactJS? In the constructor or in the render method or as a property initializer?
- What is the better way to update other state variable which is dependent on another state variable while re-rendering?
- What is the best way to update my react state when data in database changes?
- How to update the correct state value in a parent component that gets its value from a child component using hooks in react?
- What is the proper way to handle prop-types for a component that uses object destructing and object rest...spread to collect props
- ReactJS: What is the correct way to set a state value as array?
- What is the correct way to handle a key event using useEffect() hook which on the other hand triggers local state changes?
- Handle multiple react form input and update the state of the component
- What is the best way to create component library for both React and Preact?
- What is the correct way to call updateCachedData on a click event in a component that uses the RTKQ query?
- What is the proper way of rendering a component passed as a prop? And how to extend it?
- Is this correct way to render according to the state data using async await?
- What is the correct way to React Hook Fetch Api and store to Redux and retrieve it with Selector immediately?
- Using React Hooks, using "useState" and "useEffect", is there a way to render a state waiting till the state has a value?
- what is the correct way to add data array to the array state as well
- What is the correct way to include React in both an application and a private library? (React Invalid Hook Call warning from duplicate React)
- Is using callback for component state update from redux saga the right way
More Query from same tag
- Axios Undefined Response
- runtime.js:7 Uncaught Error: [RequireAuth] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
- React, Adding data from json to single post component
- ReactTooltip.hide doesn't hide tooltip instantly
- useQuery gql once from the db and then from the cache after the first request
- React-select: background color does not fill full width on wordWrap:"scroll"
- Eslint react-hooks/exhaustive derps recursion
- Make an API call with react-csv
- Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'push') , using useHistory
- use query hook react Apollo conditional call
- Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag
- TypeError: react__WEBPACK_IMPORTED_MODULE_0___default(...) is not a function
- ReactJS - Get children elements in the parent and looping to execute a method of each child
- Create a new fetch request on button click - SWR / React
- Call function in other child component when mouse up
- React.js: how to create search filter?
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS
- Material-ui components do not display correctly on production
- Ionic v5 React Ion-Slides: How to make a button to the next slide?
- Trouble with drag-and-drop sortable list using ReactJs and react-dnd
- why changing state does not trigger if it's related to props
- Set default values in React Hook Form
- Get router value from url in react
- "TypeError: Object(...) is not a function" error when trying to use react hooks (alpha)
- Accessing Const Variable in a Function - React
- How can I wait a heavy function in method of React component with showing spinner?
- How can I get the width of a ChildNode in React?
- How to show button while hover over box using react?
- React, Formik Field Arrays - mapping over repeatable fields
- useEffect doesn't set the current state from LocalStorage