Skip to content

Execute submit on every valid change (onSubmit when valid input after onChange) #7063

Answered by bluebill1049
bennettdams asked this question in Q&A
Discussion options

You must be logged in to vote

Not sure if this will solve your problem: https://codesandbox.io/s/quiet-pine-chfmz?file=/src/App.tsx

import * as React from "react";
import { useForm } from "react-hook-form";
import Headers from "./Header";
import "./styles.css";

type FormValues = {
  firstName: string;
};

let renderCount = 0;

export default function App() {
  const {
    register,
    handleSubmit,
    watch,
    formState,
    formState: { isValidating }
  } = useForm<FormValues>({
    mode: "onChange"
  });
  const onSubmit = (data: FormValues) => console.log(data);
  renderCount++;

  const data = watch();

  React.useEffect(() => {
    if (formState.isValid && !isValidating) {
      console.log(data);
    }
  }, [

Replies: 3 comments 12 replies

Comment options

You must be logged in to vote
1 reply
@bennettdams
Comment options

Comment options

You must be logged in to vote
10 replies
@TempleClause
Comment options

@k-zakhariy
Comment options

@musjj
Comment options

@aleksa-poly-ai
Comment options

@CerealeZ
Comment options

Answer selected by bennettdams
Comment options

You must be logged in to vote
1 reply
@TempleClause
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
9 participants