react-hook-form:从 api 端点获取响应后如何显示验证消息?

Nav*_*dey 5 reactjs react-hook-form

为了验证以react-hook-form形式提交的输入,我正在做这样的事情

<input type="text" id="name" ref={register({ required: true, maxLength: 30 })} />
Run Code Online (Sandbox Code Playgroud)

但这是在提交时..在我从远程API端点获得响应后,我需要在同一输入字段中显示一些验证消息..

Ast*_*nca 9

您可以使用触发器函数以编程方式触发验证:

const { trigger } = useForm();

const triggerValidation = () => {
 trigger() // all fields
 trigger("lastName") // specific field
}
Run Code Online (Sandbox Code Playgroud)

文档: https: //react-hook-form.com/api/#trigger