In this tutorial, we will learn how to use the useState Hook in Functional components in a React App.
Hooks are a new addition in React 16.8. They allow you to use state, life cycle methods and other React features without writing a class component. If we are using hooks we can have only a functional component in the entire application.
React useState Hook Overview
The useState is a Hook (function) that allows you to have state variables in functional components. To use hooks, first, we should import the useState hooks from react. The useState is a function that takes one argument and returns a current state and a function that lets you update it.
The syntax for using the useState hook is as follows:
const [state, setState] = useState(initialValue);
It takes an initial value as an argument and returns an array with two elements: the current state value and a function to update that value.
Example 1: Simple Example using useState Hook
import { useState } from "react";
function Employee() {
const [firstName, setFirstName] = useState('Ramesh')
const [lastName, setLastName] = useState('Fadatare')
const [email, setEmail] = useState('ramesh@gmail.com')
function updateEmployee(){
setFirstName('Ram');
setLastName('Jadhv')
setEmail('ram@gmail.com')
}
return (
<div>
<h1> Employee Details</h1> <hr />
<p>{firstName}</p>
<p>{lastName}</p>
<p>{email}</p>
<button onClick={updateEmployee}>Update Employee</button>
</div>
)
}
export default Employee
The useState hook returns an array with two elements: the current state value and a function to update that value. The initial state values are provided as arguments to useState. Here, the initial values for firstName, lastName, and email are set.
const [firstName, setFirstName] = useState('Ramesh')
const [lastName, setLastName] = useState('Fadatare')
const [email, setEmail] = useState('ramesh@gmail.com')
The updateEmployee function is defined within the Employee component. It is triggered when the "Update Employee" button is clicked. Inside this function, the setFirstName, setLastName, and setEmail functions are used to update the respective state variables.
function updateEmployee(){
setFirstName('Ram');
setLastName('Jadhv')
setEmail('ram@gmail.com')
}
Inside the return statement, the component renders the employee details (first name, last name, and email) using the state variables:
<h1> Employee Details</h1> <hr />
<p>{firstName}</p>
<p>{lastName}</p>
<p>{email}</p>
Example 2: Define State Object using useState Hook
import { useState } from "react";
function Employee() {
const [employee, setEmployee] = useState({
firstName: 'Ramesh',
lastName: 'Fadatare',
email: 'ramesh@gmail.com'
});
function updateEmployee(){
setEmployee({
firstName: "ram",
lastName: "jadhav",
email: 'ram@gmail.com'
})
}
return (
<div>
<h1> Employee Details</h1> <hr />
<p>{employee.firstName}</p>
<p>{employee.lastName}</p>
<p>{employee.email}</p>
<button onClick={updateEmployee}>Update Employee</button>
</div>
)
}
export default Employee
In this above example, we use defining and initializing state object using the useState hook in a functional component:
const [employee, setEmployee] = useState({
firstName: 'Ramesh',
lastName: 'Fadatare',
email: 'ramesh@gmail.com'
});
Example 3: Define State Array using useState Hook
const Employee = () => {
const dummyData = [
{"id":1,"firstName":"Ramesh","lastName":"Fadatare","email":"ramesh124fadatare@gmail.com"},
{"id":2,"firstName":"Ram","lastName":"Jadhav","email":"ram@gmail.com"},
{"id":3,"firstName":"Tony","lastName":"Stark","email":"tony@gmail.com"}
];
const [employees, setEmployees] = useState(dummyData)
return (
<div className = "container">
<h2 className = "text-center"> List Employees </h2>
<table className="table table-bordered table-striped">
<thead>
<th> Employee Id </th>
<th> Employee First Name </th>
<th> Employee Last Name </th>
<th> Employee Email Id </th>
</thead>
<tbody>
{
employees.map(
employee =>
<tr key = {employee.id}>
<td> {employee.id} </td>
<td> {employee.firstName} </td>
<td>{employee.lastName}</td>
<td>{employee.email}</td>
</tr>
)
}
</tbody>
</table>
</div>
)
}
export default Employee
const dummyData = [
{"id":1,"firstName":"Ramesh","lastName":"Fadatare","email":"ramesh124fadatare@gmail.com"},
{"id":2,"firstName":"Ram","lastName":"Jadhav","email":"ram@gmail.com"},
{"id":3,"firstName":"Tony","lastName":"Stark","email":"tony@gmail.com"}
];
const [employees, setEmployees] = useState(dummyData)
Related Tutorials
- React Hooks - useState and useEffect
- React JS ( React Hooks) + Spring Boot Tutorial
- Create a New React App Step by Step
- Understanding React App Folder Structure
- How to Add Bootstrap to React App
- ReactJS Axios GET, POST, PUT, and DELETE Example Tutorial
- ReactJS Axios Example
- ReactJS Fetch API Example
- React JS CRUD Example Tutorial
- React Router Step-By-Step Tutorial
- React Class Components
- React Functional Components
- React Props
- React State and setState in Class Components
- React useState Hook
- React Conditional Rendering
- How to Add Bootstrap in React JS Using NPM
- How to Create React App using Vite
- Handling Events in React
- How to Consume REST API in React
- React JS Form Validation Example
Comments
Post a Comment
Leave Comment