首页 > 解决方案 > React-select & creatable select with reatc-hook-form 专注于验证错误

问题描述

我在 react-hook-form 中使用创建选择组件。我已经尝试过 react-select 和 creatable-react-select 两者,我在这个问题的上下文中遇到的行为是相同的。

使用最新的 react-hook-form (7.6.1),当字段绑定错误时,我无法将字段设置为焦点。 为此编写沙箱示例

当我使用旧版本的 react-hook-form 时,我可以让这种行为按预期工作。 为此编写沙箱示例

因为我希望这种行为在最新的 react-hook-form 中起作用,所以对第一个示例中的问题有什么建议?

标签: javascriptreactjsfrontendreact-selectreact-hook-form

解决方案


对 react-select 组件使用控制器,但没有使用 Controller 的“render”方法中的 prop“field”,因此,未设置 ref 元素以帮助 react-hook-form 设置焦点。

工作示例:代码沙箱


推荐阅读