Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Forms in React JS

User image

Published by

sanya sanya

Published at: 28th Jul, 2023
1.68 mins read

In React, forms are created using the "form" element and managed using state. React provides a way to handle form inputs and their changes through the concept of controlled components, where the component state represents the value of the form input.

The example for how we can create a form in React JS is mentioned below -

import React, { useState } from 'react';

 function MyFormComponent() {
   const [name, setName] = useState('');
   const [email, setEmail] = useState('');

   const handleSubmit = event => {
     event.preventDefault();
     // Do something with the form data, e.g., submit      it to a server
     console.log('Name:', name);
console.log('Email:', email);
};

   return (
     <form onSubmit={handleSubmit}>
       <label>
         Name:
    <input
      type="text"
      value={name}
      onChange={event => setName(event.target.value)}
    />
  </label>
  <br />
  <label>
    Email:
    <input
      type="email"
      value={email}
      onChange={event => setEmail(event.target.value)}
    />
  </label>
  <br />
  <button type="submit">Submit</button>
</form>
);
 }

function App() {
  return <MyFormComponent />;
}

export default App;

In the above example, we create a form component called MyFormComponent. It maintains the form state using the useState hook.The two values of these inputs are controlled by the component's state variables name and email.




The onChange event handler is attached to each input, which updates the corresponding state variable (name or email) with the new value whenever the input changes. This ensures that the component's state always reflects the current value of the form inputs.

The handleSubmit function must be called when the form is submitted because it prevents the default form submission behavior, which will lead to a page reload. Instead, we can perform any desired actions, such as sending the form data to a server or performing local validation.

In the handleSubmit function, we can access the form data using the state variables. We can perform further processing or send the data to an API endpoint.

Remember to include the event.preventDefault() statement in the handleSubmit function to prevent the default form submission behavior.

By managing the form state and handling input changes, we can create interactive and controlled forms in React.

Library

WEB DEVELOPMENT

Basic

Frontend

Express JS

React

Hello World in ReactJS

Rendering Elements

States and Lifecycles in React JS

Handling Events in React JS

Introduction to JSX

Components and Props in React JS

Conditional Rendering

Lists and Keys in React JS

Introduction to Redux

Types of components (class vs functional)

Using the useEffect hook

Building custom hooks in React JS

Context API in React JS

Forms in React JS

Lifting State Up in React JS

Composition vs Inheritance

Prop drilling in React JS

Introduction to Hooks

Using the useState hook

UseMemo vs useCallback

Backend

Interview Questions

FAANG QUESTIONS