How to pass dynamic input field value to state as an array and update the value when user changes the input again?
Input field was generated by JSON data and in the state, we cannot define the initial value for all. Can anyone give some advice for this kind of problem?
this.state = {
file:[{value1,value2}]
};
and input field is generated like
{this.state.language.map((item,index) =>
<div className="be-checkbox inline" key={index} >
<input type="text" onChange={this.handleChange.bind(this, index)} value={this.state.index} />
</div>
)}
Add onChange and value in every input like below.
<input type="text" onChange={this.handleChange.bind(this, index)} value={this.state.index}/>
Add the function to be called
handleChange(name, e){
var change = {};
change[name] = e.target.value;
this.setState(change);
}
const [data, setData] = useState({});
//create an onInputChange function for the inputs this way
const onInputChange = async e => {
const {name, value} = e.target;
//check to validate if entry is not a number
if(isNaN(name)){
data[name] = value;
//somehow update data
setData({...data})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With