Skip to content
Discussion options

You must be logged in to vote

All you have to do is pass the type of the form data you're expecting to useFormContext as in the following example:

import React from "react";
import { Controller, useForm, FormProvider, useFormContext } from "react-hook-form";

type FormData = {
  name: string;
  age: number;
}

export default function App() {
  const methods = useForm<FormData>();
  const onSubmit = (data: FormData) => console.log(data);

  return (
    <FormProvider {...methods}>
      // pass all methods into the context
      <form onSubmit={methods.handleSubmit(onSubmit)}>
        <NestedInput />
        <input type="submit" />
      </form>
    </FormProvider>
  )
}

function NestedInput() {
  const { control } = u…

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
4 replies
@chandu
Comment options

@bluebill1049
Comment options

@abhinandkr56
Comment options

@MetaMmodern
Comment options

Comment options

You must be logged in to vote
3 replies
@bbreckler
Comment options

@fasih-mehmood
Comment options

@andrastoth-ws
Comment options

Answer selected by chandu
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
8 participants