Hooks were introduced in version 16.8 of React to enhance functional components, allowing them to manage state and other side effects traditionally handled in class components. One of these powerful Hooks is the useEffect
hook in React functional components.
The React useEffect
hook enables functional components to perform side effects at specific points in a component’s lifecycle:
- When a component is first rendered (similar to
componentDidMount
function in class components)
- When the component updates due to changes in specific values (similar to
componentDidUpdate
function in class components)
- Just before the component is removed from the DOM (similar to
componentWillUnmount
function in class components)
With React useEffect
, we can execute side-effect tasks like fetching data in React using fetch API, manipulating the DOM, or subscribing to events. We can also use multiple useEffect
hooks within a component to separate different logic for better readability and maintainability.
Syntax
useEffect(callback, [dependencies]);
In the above syntax:
-
callback
refers to the function that runs when the useEffect
hook in React is triggered. This can be an arrow function.
-
dependencies
is an optional array of variables that useEffect
monitors. The hook re-runs only if any of the variables in this array change.
Controlling useEffect
hook with dependencies
The dependencies
array in React useEffect
determines when useEffect
hook should re-run. Here’s how different values for dependencies
control the behavior:
Run useEffect
on every render
If we want useEffect
to execute on every render, simply omit the dependencies
array:
useEffect(() => {
// This code runs on every render
});
Run useEffect
only once (on initial render)
To make React useEffect
hook run only once when the component is initially mounted, pass an empty array []
as dependencies
. This mimics the componentDidMount
method in React class components:
useEffect(() => {
// This code runs only on the initial render
}, []);
Run useEffect
when certain values change
To trigger useEffect
hook only when specific variables change, include them in the dependencies
array. This is similar to componentDidUpdate
method in React class components:
useEffect(() => {
// This code runs only when variable x changes
}, [x]);
Run cleanup code before component unmounts
To run a cleanup function before the component is removed from the DOM, return a function from within the callback function. This cleanup function in useEffect
hook runs after the initial render and before every re-run of useEffect
, ensuring that any previous side effects are cleared. This functionality is similar to componentWillUnmount
method in React class components:
useEffect(() => {
// Setup code
return () => {
// Cleanup code that runs before the component unmounts
};
});
How to use useEffect
in a chatbox component?
Consider a simple example of a chatbox component in React where we need to load previous messages using the useEffect
hook and indicate that the user is active when the chatbox is first opened.