score:4
Accepted answer
loop through the number entered by the user.
const inputs = [];
for (let i = 1; i <= this.state.total; i++) {
inputs.push(
<input name={`input-${i}`} onchange={this.onchange} />
)
}
render inputs
render() {
return (
....
{inputs}
...
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<div id="root"></div>
<script type="text/babel">
class app extends react.component {
constructor() {
super();
this.state = {
name: 'react',
total: 0,
totalinput: '',
show: false,
};
}
add = () => {
this.setstate({
total: this.state.totalinput
})
}
showvalues = () => {
this.setstate({
show: true
})
}
onchange = (event) => {
this.setstate({
[event.target.name]: event.target.value
})
}
render() {
const inputs = [];
for (let i = 1; i <= this.state.total; i++) {
inputs.push(
<input name={`input-${i}`} onchange={this.onchange} />
)
}
return (
<div>
<input onchange={(e) => this.setstate({ totalinput: e.target.value})} value={this.state.totalinput} placeholder="enter number" />
<button onclick={this.add}>add</button>
<br />
{inputs}
<br />
<button onclick={this.showvalues}>show inputs values</button>
{ this.state.show &&
<pre>{json.stringify(this.state, null, 4)}</pre>
}
</div>
);
}
}
reactdom.render(<app />, document.getelementbyid('root'));
</script>
score:2
here is the solution. other refactor may be required.
working link https://jsfiddle.net/ogmv3wpu/1/
class hello extends react.component {
constructor() {
super();
this.state= {
inputsize: 0
}
}
handleonchange(value){
this.setstate({
inputsize: value.target.value
});
}
renderinputs(value){
const inputs=[];
for(let i=0; i<value; i++){
inputs.push(<div key={i}><input value={name}}type="text" name="quantity"/></div>)
}
return inputs;
}
render() {
console.log(this.state.inputsize);
return (<div>
<input type="number" name="quantity" min="0" max="99999" onchange={(value)=>this.handleonchange(value)}/>
<div>
{this.renderinputs(this.state.inputsize)}
</div>
</div>
)
}
}
reactdom.render(
<hello name="world" />,
document.getelementbyid('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="container">
<!-- this element's contents will be replaced with your component. -->
</div>
Source: stackoverflow.com
Related Query
- React - dynamic input fields based on count
- React - Dynamic form fields where fields change based on a dropdown menu (using hooks)
- How to create dynamic form input fields in React with ANTd
- Dynamic input fields in React using functional component
- Disabled button based on React input fields not working properly
- React onChange rerender dynamic input fields strange
- Display multiple dynamic input fields in react, based on a changing state variable?
- How can I add dynamic fields based on my selected attributes in react js?
- React Form Dynamic Input Fields
- Dynamic Editable input fields in react js
- Build dynamic URLs in React based on input
- Setting states for dynamic input fields in react
- Generate Dynamic year wise tables based on input dates in react Js
- React / useState (complex) - dynamic input fields and fecthed data
- React dynamic form input fields typeError undefined trim()
- How to dynamically display a number of input fields based on a number given on a previous field on that page in react
- How to display dropdown fields based on column count and manage state individually for each dropdwon in react js?
- DefaultValues of react-hook-form is not setting the values to the Input fields in React JS
- React Iterate and insert components based on count of array
- TypeScript & React - one onChange Handler for multiple input fields
- How to clear input values of dynamic form in react
- How to get form data from input fields in React
- Validation of form input fields in React
- React updating state in two input fields from form submission
- Filter Array of Objects based on input field in React
- Change style of existing input fields whenever error occurs in React Formik
- How can i create input text fields dynamically in react js - JSX?
- How to fetch data in react server side rendering based on dynamic routing path
- React Hooks dynamic input
- Abstracting React Hooks for dynamic form fields
More Query from same tag
- Union type assignment issue with React/Redux/Typescript setup
- React, state not getting updated when calling the related setState
- CSS Triangles in React Native
- Import data from async function
- How to use Real time chart in react?
- In React, how can I cause anchors to do nothing on click?
- How do I implement conditional rendering in a functional React component?
- Why passing a React state array to component make it array of one array
- post request to node server not hitting endpoint
- Changing the CSS for a React component has effect on all other pages
- ReactJS Interest-cohort
- Sticky HTML table header almost works, but rows slightly visible above header
- React: Displaying images from api call
- Multiple Modal Opening when Updating a Row using React
- How to fix the error Cannot destructure property temperatureData of 'undefined' or 'null' in react without class
- hello guys I'm new to react I was trying to display my header component but it's not show
- TypeScript Theme Switch types erros
- How to save input field in Reactjs?
- Why are my actions not dispatching when they are passed to eventhandlers
- express server with socket.io does not emit to right recipient
- How can I replace specific strings within an object?
- How to set a filename for downloading csv file with react-table?
- I'm getting a blank screen with React JS Navigation
- How to hover over one chart to display a vertical line and tooltip on all charts in D3.js?
- How to configure nextjs 9 and ant design less compatibility?
- how to get value of Material-ui auto-complete in react.js
- Using many window.open
- When testing react project npm test works but jest doesn't
- How to set QueryString parameter for test in React
- react-konva: Property does not exist on type 'never'