Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

382
Views
How to pass react-hook-form handleSubmit of Child Component to a Parent Component

I have a Parent and Child components, where latter has a form in it:

import React from "react";
import { useForm } from "react-hook-form";

function Parent() {

  const onSubmit = (data) => {
    console.log(data);
  };

  return <Child onSubmit={onSubmit} />
}

function Child({ onSubmit }) {
  const {
    register,
    handleSubmit,
  } = useForm();
  
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input defaultValue="test" {...register("example")} />
      <input type="submit" />
    </form>
  );
}

Here I need to invoke form submission from Parent Component.

While I could take useForm to a Parent Component, I feel like this is against encapsulation principle since useForm is used in Child component, it should be there ideally:

function Parent() {
  const {
    register,
    handleSubmit,
  } = useForm();

  const onSubmit = (data) => {
    console.log(data);
  };

  return <Child submit={handleSubmit(onSubmit)} register={register}  />
}

function Child({ submit, register }) {
 
  
  return (
    <form onSubmit={submit}>
      <input defaultValue="test" {...register("example")} />
      <input type="submit" />
    </form>
  );
}

Is there a better way to do this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!