Following is a sample code to handle change in input values
import React, { useState } from 'react'
function MyName () {
const [ formData, setFormData ] = useState(
{
name:'test',
age:29,
account: {
card:3939939393,
exp:2020
}
}
)
function handleChange1 (evt) {
setFormData({
...formData , [evt.target.name]:evt.target.value
});
}
function handleSubmit1 () {
console.log(formData);
}
return (
<div>
<h1>My name is: {formData.name}</h1>
<h1>My age is: {formData.age}</h1>
<h1>My card is: {formData.account.card}</h1>
<h1>My ex is: {formData.account.exp}</h1>
<input name="name" type="text" value={formData.name} onChange={handleChange1} />
<input name="age" type="text" value={formData.age} onChange={handleChange1} />
<input name="card" type="text" value={formData.account.card} onChange={handleChange1} />
<input name="exp" type="text" value={formData.account.exp} onChange={handleChange1} />
<button onClick={handleSubmit1}>Click</button>
<div>
</div>
</div>
)
}
export default MyName
So for this example code I was able to change the name and age values but the card and exp values I couldn't change value why ??
when I wanted to add a new value of the card and also when I wanted to add a new value of exp the input of (card, ex) became blocked