Using register from use hook for dynamic input
i am having an issue where i want to bind a form with an input that generated dynamically
when i use the register from the useform hook. all the inputs will have the same value.
![No description](https://cdn.answeroverflow.com/1043779791142256640/Screenshot_from_2022-11-20_10-39-15.png)
![No description](https://cdn.answeroverflow.com/1043779791603650600/Screenshot_from_2022-11-20_10-47-59.png)
8 Replies
rival-black•2y ago
Hey @codecrazy, when using
register
you need to make sure you pass a unique one, maybe by using selectedSocialMedia.map
🤔
you can do ...register(`username.${index}`)
or it will be better to use something else than index like you have id
, maybe that? 🤔sensitive-blue•2y ago
but how it will bind the value coming from server
for my understanding register must use the same object name
also, there will be no id if the input is not set
rival-black•2y ago
What is the shape of the data coming from the server, when these fields are multiple?
sensitive-blue•2y ago
its like {
"companySocialMedia": [{
"id"
"username":"",
"type":""
}]
}
rival-black•2y ago
Looks like the data is not accessible just with
username
. Its like companySocialMedia.${index}.username
when you pass the same name
parameter (first one) to register
, having same values is the expected behavior since this is how react-hook-form
distinguishes them 😅sensitive-blue•2y ago
is the word index a reserved word or i should get the index from the map
?
rival-black•2y ago
Not reserved, you should get that from the map or someway else.
Like, its
companySocialMedia.0.username
for the first and so on.
Please let me know if this works and if there's anything we can do to help 🙏sensitive-blue•2y ago
i will try it and let you know thanks