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:
- The
App
component renders a form with a label, an input field, and a submit button. - When the user types in the input field, the
onChange
event is triggered, updating thenewname
state with the current input value. - When the form is submitted (by clicking the submit button),
handleOnSubmit
is called. - The
e.preventDefault()
prevents the form from being submitted in the traditional way (no page reload). - The
alert
function shows the current value ofnewname
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’;
import React
: This imports the React library, which is necessary for building React components. React is a JavaScript library for creating user interfaces.{ 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 = () => {
- 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(“”);
- This line creates a piece of state called
newname
using theuseState
hook. newname
: This variable holds the current state (initially an empty string""
), which will store the user’s input.setNewame
: This is the setter function to update the statenewname
. You can callsetNewame
with a new value to change the value ofnewname
.
- const handleOnSubmit = (e) => {
handleOnSubmit
: This is an event handler function that will be called when the form is submitted (i.e., when the submit button is clicked).(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();
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}
);
alert(...)
: This displays a pop-up alert showing the message"Your Entered Value is: "
followed by the current value ofnewname
. This is how the form gives feedback to the user about their input.
- return ( <form onSubmit={handleOnSubmit}>
- The
return
statement contains the JSX that defines what this component renders. In this case, it’s a form (<form>
) that callshandleOnSubmit
when the form is submitted, thanks to theonSubmit
attribute.
- <label>Enter your name: </label>
- 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)} />
- This is a text input field. The
value
of the input is controlled by thenewname
state, which means that whatever is typed into this field will be stored innewname
. onChange={(e) => setNewame(e.target.value)}
: This is an event listener. Whenever the user types in the input field, theonChange
event is triggered. It takes the new value from the event (e.target.value
) and updates thenewname
state by callingsetNewame
.
<input type="submit" />
- This is the submit button. When clicked, it will trigger the form’s
onSubmit
event, which will callhandleOnSubmit
.
);
- This closes the
return
statement, which means the form is fully defined here.
export default App;
export default App
: This allows theApp
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 Reply
You must be logged in to post a comment.
Leave a Comment