<!DOCTYPE html>
React JS Tutorial 2024
In this tutorial, we will be discussing the useEffect hook in React Js. The useEffect hook in React allows you to perform side effects in your functional components. These side effects can include data fetching, subscriptions, or manually changing the DOM in React components.
What is the useEffect hook?
The useEffect hook is a function in React that lets you perform side effects in your functional components. It is similar to componentDidMount, componentDidUpdate, and componentWillUnmount in class components, but combined into a single API.
How to use the useEffect hook?
To use the useEffect hook in your functional components, you need to import it from the ‘react’ package. Here’s an example of how you can use the useEffect hook:
import React, { useEffect, useState } from 'react';
const MyComponent = () => {
useEffect(() => {
// Perform side effects here
console.log('Component mounted or updated');
return () => {
// Perform clean-up here
console.log('Component unmounted');
};
}, []);
return (
My Component
{/* Your component's UI */}
);
};
export default MyComponent;
In the example above, we have defined a functional component called ‘MyComponent’ that uses the useEffect hook to log messages when the component is mounted, updated, or unmounted.
Conclusion
The useEffect hook in React Js is a powerful tool that allows you to perform side effects in your functional components. By using the useEffect hook, you can easily manage side effects and keep your components clean and efficient.
That’s all for this tutorial on the useEffect hook in React Js. I hope you found it helpful. Stay tuned for more React JS tutorials in the future!
Bhayi ek series back-end ke uper jise complete MERN(MEN) SHIKHA DO