The code you provided is an example of a React registration form that submits data to an API endpoint when the user clicks the "Register" button. It utilizes the Axios library to request a POST to the specified API endpoint.
Here's a breakdown of the key parts of the code related to submitting the registration form data to the API:
submitHandler
Function:
const submitHandeler = (event) => {
event.preventDefault();
/* Stop Form Reload; */
const data = { username, name, email, phone, password, college_id };
axios.post('YOUR-API-END-POINT', data)
.then((response) => {
console.log(response);
})
.catch((error) => {
alert('Sorry Try Again');
});
};
In this function:
event.preventDefault()
prevents the default form submission behavior (which would cause a page reload).data
.axios.post
function is used to send a POST request to the specified API endpoint (in this case, 'https://example.com/app/registration/') with the data
object as the request body.Form Submission:
<form name="saveData" onSubmit={submitHandeler} className="mx-1 mx-md-4">
{/* ... Form input fields */}
<button type="submit" className="btn btn-primary btn-lg">
Register
</button>
</form>
In the form element:
onSubmit={submitHandeler}
specifies that the submitHandler
function should be called when the user submits the form.submitHandler
function is triggered.
See Full Code
import React, { useState } from 'react';
import axios from "axios";
const Register = () => {
const initialValue = '';
const [username, setUsername] = useState(initialValue);
const [name, setName] = useState(initialValue);
const [email, setEmail] = useState(initialValue);
const [phone, setPhone] = useState(initialValue);
const [cpassword, setCPassword] = useState(initialValue);
const [password, setPassword] = useState(initialValue);
const [college_id, setCollege_id] = useState(initialValue);
const handleChange = (event) => {
const { name, value } = event.target;
switch (name) {
case 'username':
setUsername(value);
break;
case 'name':
setName(value);
break;
case 'email':
setEmail(value);
break;
case 'phone':
setPhone(value);
break;
case 'cpassword':
setCPassword(value);
break;
case 'password':
setPassword(value);
break;
case 'college_id':
setCollege_id(value);
break;
default:
break;
}
};
const submitHandeler=(event)=>{
event.preventDefault();
const data={username,name,email,phone,password,college_id};
axios.post('YOUR-API-END-POINT', data)
.then((response) => {
console.log(response);
})
.catch((error) => {
alert('Sorry Try Again');
});
};
return(
<>
<div>
<section className="vh-100" style={{ backgroundColor: '#eee' }}>
<div className="container h-100">
<div className="row d-flex justify-content-center align-items-center h-100">
<div className="col-lg-12 col-xl-11">
<div className="card text-black" style={{ borderRadius: '25px' }}>
<div className="card-body p-md-5">
<div className="row justify-content-center">
<div className="col-md-10 col-lg-6 col-xl-5 order-2 order-lg-1">
<p className="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">Sign up</p>
<form name="saveData" onSubmit={submitHandeler} className="mx-1 mx-md-4">
<div className="mb-4">
<i className="fas fa-user fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="text" id="username" name="username" className="form-control" value={username} onChange={handleChange} />
<label className="form-label" htmlFor="username">
Username
</label>
</div>
</div>
<div className="mb-4">
<i className="fas fa-user fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="text" id="name" name="name" className="form-control" value={name} onChange={handleChange}/>
<label className="form-label" htmlFor="name">
name
</label>
</div>
</div>
<div className="mb-4">
<i className="fas fa-envelope fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="email" id="email" name="email" className="form-control" value={email} onChange={handleChange} />
<label className="form-label" htmlFor="form3Example3c">
Your Email
</label>
</div>
</div>
<div className="mb-4">
<i className="fas fa-envelope fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="text" id="phone" name="phone" className="form-control" value={phone} onChange={handleChange} />
<label className="form-label" htmlFor="form3Example3c">
Contact no
</label>
</div>
</div>
<div className="mb-4">
<i className="fas fa-lock fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="password" id="cpassword" name="cpassword" value={cpassword} onChange={handleChange} className="form-control" />
<label className="form-label" htmlFor="cpassword">
Password
</label>
</div>
</div>
<div className="mb-4">
<i className="fas fa-key fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="password" id="password" name="password" className="form-control" value={password} onChange={handleChange} />
<label className="form-label" htmlFor="password">
Repeat your password
</label>
</div>
</div>
<div className="mb-4">
<i className="fas fa-key fa-lg me-3 fa-fw"></i>
<div className="form-outline flex-fill">
<input type="text" id="college_id" name="college_id" className="form-control" value={college_id} onChange={handleChange} />
<label className="form-label" htmlFor="college_id">
College
</label>
</div>
</div>
<div className="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
<button type="submit" className="btn btn-primary btn-lg">
Register
</button>
</div>
</form>
</div>
<div className="col-md-10 col-lg-6 col-xl-7 d-flex align-items-center order-1 order-lg-2">
<img
src="https://mdbcdn.b-cdn.net/img/Photos/new-templates/bootstrap-registration/draw1.webp"
className="img-fluid"
alt="Sample image"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</>
)
};
export default Register;
Import statements:
import React, { useState } from 'react';
: This line imports the React
library and the useState
hook from the 'react' module. useState
is used to manage the component's state.Component definition:
const Register = () => { ... }
: This code defines a functional React component named Register
. It's a stateless functional component.State initialization:
useState
hook for various form input fields such as username
, name
, email
, phone
, cpassword
, password
, and college_id
. The initialValue
variable is used to set the initial state value for these fields.handleChange
function:
handleChange
function is used to update the state of the respective input fields when their values change. It uses the event.target
to get the name
and value
of the input field and updates the corresponding state variable.submitHandler
function:
submitHandler
function is called when the user submits the form. It prevents the default form submission behavior using event.preventDefault()
. It then creates an object data
with the values of the form fields (username
, name
, email
, phone
, password
, and college_id
) and sends a POST request to a specific URL using the Axios library. The response is logged to the console on success, and an alert is displayed on failure.JSX code:
return
statement contains the JSX structure for the registration form. It's a form wrapped in a Bootstrap card, and it includes various input fields for the user to enter their information. The value
and onChange
attributes of the input fields are bound to the respective state variables and the handleChange
function to keep the state synchronized with the form inputs. When the user submits the form, the submitHandler
the function is called.Export:
Register
the component is exported using export default Register;
, making it available for use in other parts of the application.
export Switch (imported as Switch) was not found in react-router-dom. This problem comes from the ro..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !