[英]React form hook setValue overwrites placeholder value
I have an edit user form.我有一个编辑用户表单。 When I get data from API, I am able to setValue of the form field Array.当我从 API 获取数据时,我能够设置表单字段数组的值。 But when I setValue of a formField
ie first name or last name, it overwrites the placeholder value.但是当我设置formField
的值即名字或姓氏时,它会覆盖占位符值。 But in the case useFieldArray
it works fine.但在useFieldArray
的情况下它工作正常。 Please find the form updating code as below -请找到如下表格更新代码 -
const handleGetUser = (user: UsersDataSourceModel) => {
if (!_.isEmpty(user)) {
setValue('firstName', user.firstName, {shouldTouch: true});
setValue('lastName', user.lastName, {shouldTouch: true});
setValue('emailId', user.emailId, {shouldTouch: true});
if (user.roles.length) {
user.roles.map((role, index) => {
setValue(`userCountryRoleMappings.${index}.region`, role.region);
setValue(`userCountryRoleMappings.${index}.roleId`, role.roleId);
setValue(`userCountryRoleMappings.${index}.countryId`, role.countryId);
setValue(`userCountryRoleMappings.${index}.neverExpireRole`, role.neverExpireRole);
setValue(`userCountryRoleMappings.${index}.roleExpiryDate`, role.roleExpiryDate);
setValue(`userCountryRoleMappings.${index}.isReadOnly`, role.isReadOnly);
})
}
}
}
Pleae refer the image attached.请参考所附图片。 It is how looking when edit form page is visited.这是访问编辑表单页面时的样子。
Any help is appreciated!任何帮助表示赞赏! Thanks a ton!万分感谢!
Thanks for the one more clue.感谢您提供更多线索。
I found a similar issue that was reported on react-hook-form
about setValue
and placeholder
view simultaneously.我发现在react-hook-form
上同时报告了一个关于setValue
和placeholder
view 的类似问题。
https://github.com/react-hook-form/react-hook-form/issues/1556 https://github.com/react-hook-form/react-hook-form/issues/1556
From the link above, check out @devrnt
's comment.从上面的链接中,查看@devrnt
的评论。 You mat find it helpful.你会发现它很有帮助。
I copied his comment below just for the sake of your time.为了您的时间,我在下面复制了他的评论。
Sandbox 1 Seems like a related issue: mui/material-ui#718 I would suggest to watch on the 'TextFieldTwo' value and manually set the shrink option.沙盒 1 似乎是一个相关问题: mui/material-ui#718我建议观察“TextFieldTwo”值并手动设置收缩选项。 https://codesandbox.io/s/flamboyant-mountain-9532t https://codesandbox.io/s/flamboyant-mountain-9532t
Sandbox 2 Can be fixed by setting the mode in useForm to onChange.沙箱 2 可以通过将 useForm 中的模式设置为 onChange 来修复。 Do mind that this often comes with a significant impact on performances.请注意,这通常会对性能产生重大影响。 https://codesandbox.io/s/setremoteinputcontroller-wwsdt https://codesandbox.io/s/setremoteinputcontroller-wwsdt
So when you use material, yup and react-hook-form there are some problems.所以当你使用 material、yup 和 react-hook-form 时会出现一些问题。 While looking at material ui and its component, I came across this below prop -在查看材料 ui 及其组件时,我遇到了以下道具 -
InputLabelProps={{ shrink: true }}
So if you have any textField, you should use it like -所以如果你有任何 textField,你应该像这样使用它 -
<TextField
InputLabelProps={{ shrink: true }}
size="small"
fullWidth
{...register('lastName')}
required
label="Last Name"
variant="outlined"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.