How to Build a Simple React Form with useState Hook: Step-by-Step Guide

import React, { useState } from ‘react’

const App = () => {

const [newname, setNewame] = useState(“”);

const handleOnSubmit = (e) => {

e.preventDefault();

console.log(‘handleOnSubmit’, newname);

alert(`Your Entered Value is: ${newname}`);

}

return (

<form onSubmit={handleOnSubmit}>

<label>Enter your name: </label>

<input type=”text”

value={newname}

onChange={(e) => { console.log(“words”, e.target.value);

setNewame(e.target.value)}}

/>

<input type=”submit” />

</form>

)

}

export default App

How the Code Works:

  1. The App component renders a form with a label, an input field, and a submit button.
  2. When the user types in the input field, the onChange event is triggered, updating the newname state with the current input value.
  3. When the form is submitted (by clicking the submit button), handleOnSubmit is called.
  4. The e.preventDefault() prevents the form from being submitted in the traditional way (no page reload).
  5. The alert function shows the current value of newname in a pop-up window, allowing the user to see what they typed.

Certainly! Let’s break down your code step by step:

  • import React, { useState } from ‘react’;
  1. import React: This imports the React library, which is necessary for building React components. React is a JavaScript library for creating user interfaces.
  2. { useState }: useState is a React Hook that allows you to add state to functional components. This means you can store and manage data (like the user input) within the component.
  • const App = () => {
  1. This is a functional component named App. In React, components are functions or classes that return JSX (HTML-like syntax) that defines the structure and behavior of the UI.
  • const [newname, setNewame] = useState(“”);
  1. This line creates a piece of state called newname using the useState hook.
  2. newname: This variable holds the current state (initially an empty string ""), which will store the user’s input.
  3. setNewame: This is the setter function to update the state newname. You can call setNewame with a new value to change the value of newname.
  • const handleOnSubmit = (e) => {
  1. handleOnSubmit: This is an event handler function that will be called when the form is submitted (i.e., when the submit button is clicked).
  2. (e): This is the event object, passed automatically by React when the form is submitted. It contains information about the event, such as which element triggered it.
  • e.preventDefault();
  1. e.preventDefault(): This prevents the default form submission behavior, which would cause the page to reload. By using this, we can handle the form submission with JavaScript instead of sending a request to the server.
  • alert(Your Entered Value is: ${newname});
  1. alert(...): This displays a pop-up alert showing the message "Your Entered Value is: " followed by the current value of newname. This is how the form gives feedback to the user about their input.
  • return ( <form onSubmit={handleOnSubmit}>
  1. The return statement contains the JSX that defines what this component renders. In this case, it’s a form (<form>) that calls handleOnSubmit when the form is submitted, thanks to the onSubmit attribute.
  • <label>Enter your name: </label>
  1. A label is displayed in the UI with the text "Enter your name:". This tells the user what the input field is for.
  • <input type=”text” value={newname} onChange={(e) => setNewame(e.target.value)} />
  1. This is a text input field. The value of the input is controlled by the newname state, which means that whatever is typed into this field will be stored in newname.
  2. onChange={(e) => setNewame(e.target.value)}: This is an event listener. Whenever the user types in the input field, the onChange event is triggered. It takes the new value from the event (e.target.value) and updates the newname state by calling setNewame.
  • <input type="submit" />
  1. This is the submit button. When clicked, it will trigger the form’s onSubmit event, which will call handleOnSubmit.
  • );
  1. This closes the return statement, which means the form is fully defined here.
  • export default App;
  1. export default App: This allows the App component to be used in other files. This is how you export the component so that it can be imported and used elsewhere in the project.

Leave a Comment

Leave a Reply