bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/React/React Core Practice
React•React Core Practice

React Submit Forms

Submitting Forms

You can control the submit action by adding an event handler in the onSubmit attribute for the <form>

Example:

Add a submit button and an event handler in the onSubmit attribute:

import { useState } from 'react';
import { createRoot } from 'react-dom/client';
function MyForm() {
const [name, setName] = useState("");
function handleChange(e) {
setName(e.target.value);
}
function handleSubmit(e) {
e.preventDefault();
alert(name);
}
return (
<form onSubmit={handleSubmit}>

Formula

< label > Enter your name:

<input type="text"

value={name}
onChange={handleChange}

/> </label> <input type="submit" /> </form> )

}

createRoot(document.getElementById('root')).render( <MyForm />

);

Run

Example »

Previous

React Forms

Next

React forwardRef