I catch the input values, and add them to the date object. The problem is that the mail field is included in an internal object (data{data{mail:""}}). Below is my code, I don’t understand how to add a value to an internal object.
const data = {object_type: "person", name: "", object_code: "", data: {}} //add here
const [inputData, setInputData] = useState(data)
const handleChange = (e) => {
setInputData({...inputData, [e.target.name]: e.target.value})
console.log(e.target.name)
}
const handleSubmit = (e) => {
e.preventDefault();
axios.post(url, inputData)
.then((response)=> {
console.log(response)
})
}
<form onSubmit={handleSubmit}>
<label htmlFor="create-form-name">
Имя
<input id="name" type="text" name="name" value={inputData.name} onChange={handleChange}/>
</label>
<label htmlFor="create-form-name">
Телефон
<input id="phone" type="tel" name="object_code" value={inputData.object_code} onChange={handleChange}/>
</label>
<label htmlFor="create-form-name">
Электронный адрес
<input id="mail" type="email" name={data.data.mail} /* value={inputData.data.data} */ onChange={handleChange}/>
</label>
<button className="button-create-contact" type="submit">Создать</button>
</form>
I thought about making more constants, but it seems like a bad practice
2
Answers
You can try this-