React Form Submit Example: Handling Data Submission

Ruth Jul 09, 2026

When it comes to building interactive web applications, React, a JavaScript library, is a popular choice. One common task in React development is handling form submissions. In this guide, we'll explore a practical example of form submission in React, ensuring your application responds appropriately to user inputs.

React Formik Tutorial with Yup (React Form Validation)
React Formik Tutorial with Yup (React Form Validation)

Before diving into the example, let's briefly discuss why form submission is crucial. Forms are a fundamental part of web interfaces, enabling users to input data. Proper handling of form submissions ensures that user data is collected, validated, and processed correctly, enhancing the overall user experience.

Website Contact Form
Website Contact Form

Setting Up the React Form

First, let's create a simple React form using functional components and hooks. We'll use the `useState` hook to manage the form data.

Create theme with zustand in react dark theme
Create theme with zustand in react dark theme

Here's a basic example of a form with a text input and a submit button:

```jsx import React, { useState } from 'react'; function Form() { const [name, setName] = useState(''); const handleSubmit = (e) => { e.preventDefault(); // Handle form submission here }; return (

); } export default Form; ```

Preventing Form Refresh

React Modal with react context api
React Modal with react context api

The `e.preventDefault()` method is used to prevent the default form submission behavior, which is to refresh the page.

In the example above, `e.preventDefault()` is called inside the `handleSubmit` function to prevent the page from refreshing when the form is submitted.

Handling Form Submission

How to create a login form in react
How to create a login form in react

Now, let's handle the form submission by logging the input value to the console. We'll also add some basic validation to ensure the input field is not empty.

Update the `handleSubmit` function as follows:

```jsx const handleSubmit = (e) => { e.preventDefault(); if (name.trim()) { console.log(`Form submitted with name: ${name}`); } else { alert('Name field cannot be empty'); } }; ```

Integrating Form Submission with API

React UI kit - Outlined inputs
React UI kit - Outlined inputs

In a real-world application, you would typically send the form data to a server. We can simulate this using the `fetch` API to send a POST request to a JSONPlaceholder API.

First, install `axios` for simplifying API requests: `npm install axios`. Then, update the `handleSubmit` function:

A restraining order
A restraining order
How to submit form with out reloading page
How to submit form with out reloading page
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
two screenshots showing the settings for different types of web pages, including one with an admink
two screenshots showing the settings for different types of web pages, including one with an admink
Learning React: The Main Concepts
Learning React: The Main Concepts
Product reason selection form
Product reason selection form
the react button on an iphone screen, with text below it that reads use effect
the react button on an iphone screen, with text below it that reads use effect
Complete React.js Roadmap 2026 | Learn React Step by Step
Complete React.js Roadmap 2026 | Learn React Step by Step
html code for student registration form
html code for student registration form
React Cheatsheet — Master the Basics in Minutes Quick reference for React essentials! From creating components to handling state and props — this cheatsheet has you covered. Perfect for new React devs and pros needing a refresher! #ReactCheatsheet #LearnReact #ReactJS #FrontendDevelopment #WebDevTips #ReactHooks #JSX #ReactState #ReactProps #DevResources #TechTips #CodingForBeginners #savageslayer #programbereach #sohaibalikhan #sohaibbashirkhan #researchteq #savageslayerofficials #datawarlor... React Coding, Coding For Beginners, Frontend Developer, Wordpress Developer, Cute Quotes For Friends, Website Design Inspiration, Cheat Sheets, Cute Quotes, Web Development
React Cheatsheet — Master the Basics in Minutes Quick reference for React essentials! From creating components to handling state and props — this cheatsheet has you covered. Perfect for new React devs and pros needing a refresher! #ReactCheatsheet #LearnReact #ReactJS #FrontendDevelopment #WebDevTips #ReactHooks #JSX #ReactState #ReactProps #DevResources #TechTips #CodingForBeginners #savageslayer #programbereach #sohaibalikhan #sohaibbashirkhan #researchteq #savageslayerofficials #datawarlor... React Coding, Coding For Beginners, Frontend Developer, Wordpress Developer, Cute Quotes For Friends, Website Design Inspiration, Cheat Sheets, Cute Quotes, Web Development
It Software Request Form Template
It Software Request Form Template
Respond or React worksheet
Respond or React worksheet
Cleaner Codes — React Subcomponents
Cleaner Codes — React Subcomponents
a screenshot of a cell phone with the text, use state and on - change alternative
a screenshot of a cell phone with the text, use state and on - change alternative
Software Feature Request Form Template
Software Feature Request Form Template
I created the exact same app with Redux and React. Here are the differences.
I created the exact same app with Redux and React. Here are the differences.
FREE 17+ Requisition Form Samples & Templates
FREE 17+ Requisition Form Samples & Templates
Feature Request Form
Feature Request Form
React Quickstart For Beginners | Tutorials
React Quickstart For Beginners | Tutorials
Unleash the Power of React: 8 Captivating Tutorials to Elevate Your Skills
Unleash the Power of React: 8 Captivating Tutorials to Elevate Your Skills

```jsx import axios from 'axios'; const handleSubmit = async (e) => { e.preventDefault(); if (name.trim()) { try { await axios.post('https://jsonplaceholder.typicode.com/posts', { name, }); console.log('Form submitted successfully'); } catch (error) { console.error('Error submitting form:', error); } } else { alert('Name field cannot be empty'); } }; ```

Displaying Form Submission Status

To provide user feedback, let's display a message after a successful form submission. We'll use the `useState` hook to manage the submission status.

Update the component as follows:

```jsx import React, { useState } from 'react'; import axios from 'axios'; function Form() { const [name, setName] = useState(''); const [submissionStatus, setSubmissionStatus] = useState(null); const handleSubmit = async (e) => { e.preventDefault(); if (name.trim()) { try { await axios.post('https://jsonplaceholder.typicode.com/posts', { name, }); setSubmissionStatus('Form submitted successfully'); setName(''); // Clear the input field } catch (error) { setSubmissionStatus('Error submitting form. Please try again.'); } } else { setSubmissionStatus('Name field cannot be empty'); } }; return (

{submissionStatus &&

{submissionStatus}

} ); } export default Form; ```

Now, when the form is submitted, a message will appear below the form, indicating the submission status. The input field is also cleared after a successful submission.

In conclusion, handling form submissions in React involves preventing the default form behavior, handling the submission event, and providing user feedback. By following this example, you can create robust and user-friendly form submissions in your React applications. Happy coding!