score:0
the question you have asked is a bit unclear. but as far as i understand there could be 2 issues :
- change
const quantities = [...this.state.quantities];
tolet quantities = [...this.state.quantities];
since you'll be pushing new values to the array. let qua = e.target.textcontent;
not sure if this is the best way to get the value. trylet qua = e.target.value;
and pass the value param in anchor tag<a onclick={this.targetvalue} value={i+1} key={i} href="#">{i + 1}</a>
sharing the output for what you tried will definitely help to understand better.
score:0
the best way to push elements to a state array in react. you can do this
const newquantity = {name: this.props.name, quantity: qua};
this.setstate(previousstate => ({
quantities : [...previousstate.quantities, newquantity]
}));
also don’t use multiple setstates inside handler function use one that’s enough :)
Source: stackoverflow.com
Related Query
- push elements selected into one object
- How to push into an array of object using the spread operator to a specific element
- How to push an object into an array with React Hooks
- Conjoin Related Objects Into One Object Array? (javascript)
- React push data into object of arrays
- Push an object from an array into another array javascript
- Push object from JSON rest api into empty array not working in React
- how to push an item into an array if it is inside a javascript object [state.lastValue.push is not a function]
- How to push item into array of object in array with useState?
- Dynamically create arrays and push multiple elements into it in the content of a single loop
- React push object into array and map error map not a function
- Push elements into existing array in React Hooks
- React Push One State Array into Another
- how do i add each fieldname and their respective value into one json object dynamically
- push elements to object based on verifications
- When I try to push an element to a array but it takes only one rest of the elements are got removed automatically. It happen when use useState hook
- How can I take a values of one object and put them into another object
- How to push data into object
- ReactJS: updating props - turning a 17 object array of 2 key:value pairs into one with 3 key:value pairs
- Javascript merge the values of 4 arrays into one object
- I need to convert my array of objects into one object with specific keys
- Push elements to the sides Antd (align one to the left and one to the right)
- How to split strings inside object and push them into array? Javascript
- Merge Multiple Arrays Of The Same ID Into One Object
- Javascript How to push a key value pair into a nested object array
- How loop through an object array and showcase separate HTML elements one by one
- How do I push and object into a state that is an array?
- Get all object pairs into one Array (Javascript)
- How to create an object from another class, and push it into the state array?
- Cannot able to push elements into the array
More Query from same tag
- How to avoid full app reloading on onSubmit action in form?
- React Bootstrap: Element type is invalid
- Redux store changes when reload page
- ReferenceError: document is not defined when refresh nextjs page
- How do you fix the error in resolving packages when creating a react application
- Vertically Aligning Flexbox Items Correctly
- When does React create a new component instance?
- My React list item components don't render one at a time
- React State changing to false automatically without setting it
- alternate way to pass the state value of the container in which <FORM/> is rendered to the custom fields of the form(other than formcontext)
- addEventListener on home page only
- Can't understand the error with JSON.parse
- Use Array.map() to render dynamic array but only for selected values
- Since updating react-dom and react-router-dom packages: behaviors executing additional times
- React iterate nested arrays/objects with map. Is return mandatory to render each map function?
- How to Map SetState functions?
- Save contents of Div in the state
- Returning a React Element within a Functional Component
- this.state.file.name not rendering as a react element
- How do i use fetch with a result from a fetch in the same function with sync await
- Using Reach Router with nested routes fails to parse params
- Dispatch not sending the action to the reducer
- Issues with state-changes in submit method of AtlasKit Form
- Event reference is undefined on event listener callback passed as a method
- Module not found: Error: Can't resolve the typescript file when adding typescript to a CRA app
- React Redux Problem With Google Auth Sign In
- React Router not loading component
- Piping zip file from SailsJS backend to React Redux Frontend
- Antd Modal- confirm title text style
- react phone number validation