Which is the correct way to handle Form submission in React?

ManavM

In the React documentation and a bunch of examples in various projects, form submission is handled in this manner:

const FormComponent = () => {
  const [val, setVal] = useState('');

  const onChange = (e) => {
    setVal(e.target.value);
  }

  const onSubmit = (e) => {
    e.preventDefault();

    console.log(val);
    // handle submit logic using "val"
  }

  return (
    <form onSubmit={onSubmit}>
      <input type="text" value={val} onChange={onChange} />
      <input type="submit" value="Submit" />
    </form>
  )
}

While in other places I've seen code like this

const FormComponent = () => {
  const [val, setVal] = useState('');

  const onChange = (e) => {
    setVal(e.target.value);
  }

  const onSubmit = () => {
    console.log(val)
    // handle submit logic using "val"
  }

  return (
    <div>
      <input type="text" value={val} onChange={onChange} />
      <button onClick={onSubmit}>Submit</button>
    </div>
  )
}

They both seem to achieve exactly the same thing. Which is the better way of doing it?

juliomrc

The difference is not connected to React per se, but with HTML semantics. The first example is the one correct one.

There are a lot of screen tools that require correct HTML semantics, such as accessibility tools, that will work better with correct HTML.

One example you can easily test is that with the first option, pressing enter while focusing on the input will trigger the onSubmit (like it should, for a form) and on the second example it will not.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is correct way to handle form submission in beego?

Correct way to handle conditional styling in React

Handle session while form submission

Handle onclick form submission success

React Form with file submission

React - Preventing Form Submission

Form submission in React

What is the smartest way to handle "boolean" parameter passed by a form submission into a Spring MVC controller method?

Dusk does not handle POST requests the same way Laravel does when testing a form submission

Is this correct way to handle an exception?

Correct way to handle errors and display message in React using Hooks

React + SSR: Correct way to handle a component that needs to fetch data first

What is the correct way to handle HTTP response in React Native component

Django - unable to handle form submission with dynamic field

How to handle form submission with unknown number of elements?

Handle form submission on same page Spring Boot

Resume prevented form submission in React

Authenticity token only gets set after form submission in React component which throws an error in Rails

React + Redux - What's the best way to handle CRUD in a form component?

React form values not being displayed on form submission

In React/Bootstrap 4, what is the proper way to disable a button to prevent duplicate form submission?

Correct way to layout a form

What is the correct way to handle a key event using useEffect() hook which on the other hand triggers local state changes?

Correct way to handle deprecated API

Correct way to handle deadlocks in Hibernate

Correct way to handle exceptions in Java

Restore form submission state after failed submission with react hooks

How to apply useEffect based on form submission in React?

React - redirect after complete form submission