How can I get nested form elements values in ReactJS?
Example
Let say I have a form with following elements:
<form onSubmit={handleSubmit(e)}>
<input ref="name" type="text" />
{ /* possibly unlimited number of emails */ }
<input ref="email[]" type="text" />
<input ref="email[]" type="text" />
{ /* possibly unlimited number of book objects */ }
<fieldset>
<input ref="title" type="text" />
<input ref="author" type="text" />
</fieldset>
<fieldset>
<input ref="title" type="text" />
<input ref="author" type="text" />
</fieldset>
</form>
I want to get values of these fields onSubmit. The easiest way and most intuitive would be through:
this.refs
But it will flatten all refs and return only last of the array elements. So it will return something like:
{ name: '', email[]: '', title: '', author: '' }
What I want to get should look like this:
{ name: '',
email:['', ''],
books: [
{ title: '', author: '' },
{ title: '', author: '' }
]
}
Or something similar.
Maybe using refs is not the best option, but I'm not sure about any other alternatives.
EDIT Executing:
e.target.childNodes
Returns something similar to what I want to achieve but it returns plain HTML nodes so getting values out of them is quite a lot of work (writing some custom method for processing nodes tree).