I want to receive the value of some input fields and set them as the newValue state, but some of the attributes of the state are objects themselves. The format in which I want newValue state to be :
{
name : {
firstName : 'Eithan',
lastName :'Auralius'
},
rank : 7
}
Right now, the object is getting saved like :
{
name.firstName : 'Eithan',
name.lastName : 'Auralius',
rank : 7
}
Is there a way to achieve this by tweaking the getValue function or the input field?
//State to store the created or updated user string
const [newValue, setNewValue] = useState();
//onChange triggered function to update
const getValue = (event)=> {
//Setting the value of the state using the changed input fields
setNewValue((newValue)=> {
return {
...newValue,
[event.target.name] : event.target.value,
}
});
}
const submitData = (event)=> {
event.preventDefault();
console.log(newValue);
}
return (
<div className='loginRegister'>
<form onSubmit={submitData}>
<div className='descriptionInput'><div className='description'>First Name</div>
{//the name prop is used to set the field name in the getValue function}
<input type="text" name='name.firstName' defaultValue={member.name.firstName} onChange={getValue}></input></div>
<button>Submit</button>
</form>
</div>
);