I have a dynamically generated form which consists of individual components. The form may contain unlimited number of instances of each component. It is defined by map()
ing an array.
// main.js
function submitHandler() {
...
}
...
<form onSubmint={submitHandler}>
{formFields.map((item, index) => {
{{
'text': <UploadFormText name={someName} />,
'image': <UploadFormImage name={someName} />,
'video': <UploadFormVideo name={someName} />,
'file': <UploadFormFile name={someName} />,
}[item.type]}
})}
</form>
Component itself consists of the main text field with value
attribute and possible auxiliary elements. The value
is changed by direct input or by other means and is then saved with useState()
hook inside the component.
// component.js
export default function UploadFormText({ name }) {
const [text, setText] = useState('')
return (
<>
<input value={text} name={name} />
</>
)
}
How should I access value
of component.js inside main.js to validate and submit it? If there are multiple ways, which is the most easy and which is the best practice?