Sign in
Log inSign up

Pass form's submit handler as a props and also access the form's state in the submit handler.

Arun Kumar's photo
Arun Kumar
·Aug 2, 2019

Hi Folks,

I'm trying to pass submitHandler as a props to a form component using the hooks based approach. How to access the form component state in the submitHandler function in the parent?

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import MyForm from "./myForm";

function App() {
  useEffect(() => {});

  const handleSubmit = e => {
    e.preventDefault();
// How to access MyForm component's state here?
 };
  return (
    <div>
      <MyForm fname={"Arun"} lname={"Kumar"} onSubmit={handleSubmit} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);