score:0
I think you could just create one div, and an array to store the values, then create a function that everytime the user choose to add a new value, it saves the current on that array and clean the input. So, when the user select to process the data it takes that array and do what you need.
score:2
Without seeing your code it's hard to help, but you probably want to use controlled inputs rather than uncontrolled ones, and so you'd have an array of the current values for the inputs as state information.
For instance, in a functional component using hooks:
const { useState } = React;
function Example() {
// The values we use on the inputs
const [values, setValues] = useState([]);
// Update the value at the given index
const updateValue = (value, index) => {
setValues(values =>
Object.assign([], values, {[index]: value})
);
};
// Add an input
const addInput = () => {
setValues(values => [...values, ""]);
};
// Get the sum (just an example; and one of the very few places I'll use `reduce`)
const sum = values.reduce((sum, value) => sum + Number(value), 0);
// Render as many inputs as we have values, along with the
// button to add an input and the sum
return (
<div>
<div>
{values.map((value, index) =>
<div key={index}>
<input type="text" value={value} onChange={evt => updateValue(evt.target.value, index)} />
</div>
)}
</div>
<div>Sum: {sum}</div>
<input type="button" value="Add Input" onClick={addInput} />
</div>
);
}
ReactDOM.render(<Example/>, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js"></script>
Source: stackoverflow.com
Related Query
- How to loop through new input boxes renderer in React and get the values in an 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 iterate through a dictionary in React to get the values separately and display using html?
- React - How can I get the values from my multiple input and send them to my input?
- How do I get my HTML form date input to only allow specific days of the week to be chosen using JavaScript, React and HTML?
- How to loop over form inputs and get values in react
- How to get the values for dynamically generated input field in react js?
- How to loop through refs in React and get values?
- How to get label values dynamically and adding the numbers together from different input with reactjs
- How to create new divs inside for loop and show the result in React
- how to loop through data and assign values to state array in react
- How to get a login button to navigate to a new page once clicked. I have downloaded and used the React router dom
- How can I get the value of an input field and use it in URL in React
- How to loop through an array of objects and get specific key values in React?
- How to loop through and return the values of a javascript object?
- how to get the values of a checked checkbox and the output it on a input field
- How can i get the values of multi dynamic check boxes and store into a reactjs state
- react js filter through an array of objects and compare values within to get the closest value to a variable
- how can i loop through the array inside of an object. and render it in the react component
- How to pass input from React Form to Axios get request and display the request result?
- How to loop through an array and display data within the object of that array, in react and javascript
- How to switch between object keys and clone the previous values before adding new values in react
- Microsoft Graph Toolkit - How to loop through Get component result using React and default template
- In react with hooks, how do I loop through list and only get certain items?
- react - how do I get the size of a component child elements and reposition them
- How to limit the text filed length with input type number in React JS and prevent entry of E,e, -, + etc
- How to get the first and last visible date in React Big Calendar?
- How to properly make a GET call in React returning an observable (resembling the method in Angular and not using promises)?
- React - How to loop and update values in object model?
- How can I get React styleguidist to install correctly and get past the peer Dependency issues with react-simple-code-editor: React 16 & 17?
More Query from same tag
- Form validation using regex is not working TextField MUI React
- React useState hook returns old state value despite it being updated
- How to write type definitions for React HoCs
- React/JSX: Iterating through an object with key/value pairs
- Get Geocoder address search value to update state in App.js (with react-map-gl-geocoder, mapbox-gl and react-map-gl)
- UseEffect return value
- passing a date value from parent to child results in strange format in the child component
- Generate react.fragment for multiple items via function
- ReactJS: I can not get the state to update with API data when the asynchronous API data fetch is completed
- How to create a dinamic list which has an item description that shows only on clicking the item?
- Dynamically change width depending on value
- Show error as alert on firebase authentication with react native
- How can I get the value of a component I created?
- Preflight CORS error followed by 404 error
- Optional Path React Router 4
- express post request redirect me to a json page instead of showing the success or error messages toast
- How can I re-route after a sign-in using React Router in my parent component?
- React JS & Axios Render after getting response data
- How do i fix "cb is not a function" in router?
- How to use react transition group after API loads?
- Getting env variables to an NGNIX react kubernetes pod
- What is the difference between shouldComponentUpdate and componentWillUpdate in React?
- React - how to target value from a form with onClick
- Expo XDE cannot run application throws Tunnel packager not found
- TypeError: Cannot read property 'then' of undefined although function returns promise
- React Does Not Creat App Mistake. How Do I Solve?
- How to refresh Cognito session in async function that needs to return valid credentials
- Getting data from another class to main screen in react js and redux
- How do I center text over a div that dynamically changes width within another div?
- why do I always get an error message with react router