score:0
i'm not sure why, but this is how it goes:
// formvalues === 1
setformvalues(2);
// formvalues === 1
i think it's related to the way react works under the hood, or whatever. anyway, one would never update the state the way you did. this is how you can retrieve current value:
// formvalues === 1
setformvalues(2);
// formvalues === 1
setformvalues(actualvalue => {
// actualvalue === 2
return 3;
});
alternatively, just set the whole array at once.
this should do it:
setformvalues(actualvalues => [...actualvalues, { name: name, id: id }]);
Source: stackoverflow.com
Related Query
- Store multiple html elements in variable with React
- REACT: Dynamical load multiple new HTML-Elements
- How to functionally load new data for multiple React components
- How to add multiple HTML elements with their properties in react array?
- Extending HTML elements in React and TypeScript while preserving props
- React component render is called multiple times when pushing new URL
- React New Context API - Access Existing Context across Multiple Files
- Return multiple React elements in a method without a wrapper element
- How to get multiple static contexts in new CONTEXT API in React v16.6
- How to add new elements without re-rendering whole list with React
- React - How to return HTML elements in JSON
- React onChange handler is being called multiple times during page load
- Found multiple elements error in React Testing Library
- React Router doesn`t load external html files
- Render multiple elements in React Router v6.+
- Wrap multiple strings in HTML the React way
- RxJS and React multiple clicked elements to form single data array
- React - conditionally render multiple elements
- Warning: <VIEW /> is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements
- React - Converting HTML script tag to load an SDK asynchronously
- Show hide multiple elements with react
- How to create new elements with React mapping props
- React SPA force load new JS build (including mobile)
- How to display multiple elements using bootstrap grid system and React Js
- "Anonymous" HTML elements added while consolidating React Providers
- React - show hide two elements without flickering on page load
- React js : Rendering multiple index html in a single page applications
- Add and Remove HTML Elements on Button Click in React
- Getting line breaks with string literals in html elements using React
- Conditionally render multiple elements inside a react fragment
More Query from same tag
- Why is my state beeing updated before i call setState with React Hooks and how do i fix the disabled mouse pointer?
- How to align horizontally the label of TextField in React Material-UI at the center?
- key is undefined react
- Associating conditional statements with buttons rendering in reactjs
- Make a button clickable inside a drawer?
- React TypeScript - Button Onclick Triggered When Rendered
- Object not replacing using spread operator
- checkbox state is not updating on spot
- Where is this coming from: Warning: Each child in a list should have a unique "key" prop
- React-TypeScript ConextAPI error while creating context
- Can I change toolbar language in grapesjs?
- Selecting created option on menu close/select blur using creatable component
- Checking user validity before logging in
- React source code exposed in AWS amplify deployment
- Optional render of childlren
- onClick executing a function and setState in react
- How can I simplify retrieval of corresponding foreign key objects
- How to reset child component from parent using React Context and Hooks
- redirect when login is successful using react
- ReactJS: Redux state is not changing
- React.js Unresolved variable in WebStorm
- Reseting/clearing antd RangePicker (React)
- How to open multiple links from a single hyperlink from anchor tag string in React?
- Displaying multiple images using FlatList component in react native
- Add stripe elements multiple publishable keys in ReactJs
- Why are React Actions dispatched using a Return function
- Is it possible to replace DOM elements with React?
- React - adding key interferes with state of children
- Javascript syntax error when trying to rewrite css in class
- Chat application needs to scroll to bottom React/jQuery, not working