diff --git a/src/content/faqs.mdx b/src/content/faqs.mdx index b925f73a9..522614cea 100644 --- a/src/content/faqs.mdx +++ b/src/content/faqs.mdx @@ -127,7 +127,7 @@ React Hook Form needs a `ref` to collect the input value. However, you may want ```typescript copy -import React, { useRef } from "react" +import { useRef, useImperativeHandle } from "react" import { useForm } from "react-hook-form" type Inputs = { @@ -137,21 +137,21 @@ type Inputs = { export default function App() { const { register, handleSubmit } = useForm() - const firstNameRef = useRef(null) - const onSubmit = (data) => console.log(data) + const firstNameRef = useRef(null) + const onSubmit = (data: Inputs) => console.log(data) const { ref, ...rest } = register("firstName") + const onClick = () => { + firstNameRef.current!.value = "" + } + + useImperativeHandle(ref, () => firstNameRef.current) return (
- { - ref(e) - firstNameRef.current = e // you can still assign to ref - }} - /> - + +
) @@ -159,7 +159,7 @@ export default function App() { ``` ```javascript copy -import React, { useRef } from "react" +import { useRef, useImperativeHandle } from "react" import { useForm } from "react-hook-form" export default function App() { @@ -167,18 +167,19 @@ export default function App() { const firstNameRef = useRef(null) const onSubmit = (data) => console.log(data) const { ref, ...rest } = register("firstName") + const onClick = () => { + firstNameRef.current!.value = "" + } + + useImperativeHandle(ref, () => firstNameRef.current) + return (
- { - ref(e) - firstNameRef.current = e // you can still assign to ref - }} - /> - + +
)