React onSubmit Event (original) (raw)

Last Updated : 05 Aug, 2025

The onSubmit event in React is an event handler that is triggered when a form is submitted. It allows you to handle form submissions and allows you to control what happens when a user submits a form, such as form validation, preventing the default submission behaviour, and executing custom logic like sending data to an API or processing the form data.

**Syntax

When Does the onSubmit Event Get Triggered?

The onSubmit event gets triggered when

**Note: When a user submits a form, React triggers the event handler and gives you access to the form’s data via the event object.

**Now let's understand this with the help of an example:

JavaScript `

// Filename - App.js

import React, { useState } from "react"; function App() { const [value, setValue] = useState(""); const [result, setResult] = useState(""); function handleSubmit(e) { e.preventDefault(); setResult( "Form has been submitted with with Input: " + value ); }

function handleChange(e) {
    setValue(e.target.value);
    setResult("");
}
return (
    <div
        style={{ textAlign: "center", margin: "auto" }}
    >
        <h1 style={{ color: "Green" }}>
            GeeksforGeeks
        </h1>
        <h3>
            Exemple for React onSubmit Event Handler
        </h3>
        <form onSubmit={handleSubmit}>
            <label>Add input here: </label>
            <input
                value={value}
                onInput={handleChange}
                required
            />
            <br />
            <br />
            <button type="submit">Submit</button>
        </form>
        <br />
        <div>
            <h4>{result}</h4>
        </div>
    </div>
);

}

export default App;

`

**Output
Peek-2023-11-28-16-29

**In this example

Accessing the Event Object

React provides access to the event object within the onSubmit handler. This allows you to access form data, prevent default submission, and handle other event-related tasks efficiently.

**Now, let us understand with the help of the example:

JavaScript ``

import React, { useState } from "react";

function AccessEvent() { const [value, setValue] = useState(""); const handleSubmit = (event) => { console.log("Event object:", event); alert(Form submitted with value: ${value}); };

const handleChange = (event) => {
    setValue(event.target.value);
};

return (
    <div>
        <form onSubmit={handleSubmit}>
            <label>
                Enter Text:
                <input type="text" value={value} onChange={handleChange} />
            </label>
            <button type="submit">Submit</button>
        </form>
    </div>
);

}

export default AccessEvent;

``

**Output

**In this example

Preventing Default Form Submission

In React, when a form is submitted, the default behavior is for the page to reload, which is typically undesirable for single-page applications (SPA). To prevent this, we use the event.preventDefault() method. This allows us to handle the form submission programmatically without reloading the page.

**Now, let us understand with the help of the example:

JavaScript `

import React, { useState } from "react";

function PreventForm() { const [value, setValue] = useState(""); const [result, setResult] = useState("");

const handleSubmit = (event) => {
    event.preventDefault();

    if (!value.trim()) {
        alert("Input cannot be empty!");
    } else {
        setResult(value);
        alert("Form submitted successfully!");
    }
};

const handleChange = (event) => {
    setValue(event.target.value);
    setResult(""); 
};

return (
    <div>
        <form onSubmit={handleSubmit}>
            <label>
                Enter Text:
                <input type="text" value={value} onChange={handleChange} />
            </label>
            <button type="submit">Submit</button>
        </form>
        <p>Result: {result}</p>
    </div>
);

}

export default PreventForm;

`

**Output

**In this example

Key Features of onSubmit

Use Cases for onSubmit

Note: It is similar to the HTML DOM onsubmit event but uses the camelCase convention in React.

Conclusion

The **onSubmit event in **React is a powerful tool for **managing form submissions. It allows developers to control form behaviors, such as **validating input, preventing default page reloads, and performing actions like **API calls. By using **onSubmit, you can create **responsive and interactive forms, enhancing the user experience in **React applications.