Skip to content

Latest commit

 

History

History
63 lines (47 loc) · 2.56 KB

react.md

File metadata and controls

63 lines (47 loc) · 2.56 KB

useEffect

useEffect is a React hook that allows you to perform side effects in your functional components. Side effects are operations that interact with the outside world, like data fetching, DOM manipulation, and subscriptions. useEffect is used to encapsulate these side effects in a way that integrates well with React's component lifecycle.

The basic syntax of useEffect is:

useEffect(() => {
  // Side effect code
  // ...

  return () => {
    // Cleanup code (optional)
    // ...
  };
}, [dependencies]);

Let's break down the different parts of the useEffect hook:

  • The first argument is a function that contains the code you want to run as a side effect. This function will be executed after the component has rendered or whenever the dependencies specified in the second argument change.

  • The second argument is an array of dependencies. It determines when the useEffect function should be run. If any of the dependencies change between renders, the effect will re-run. If you pass an empty array ([]), the effect will run only once after the initial render. If you omit the second argument, the effect will run after every render.

  • Inside the effect function, you can perform any side effect you want, such as fetching data from an API, updating the DOM, subscribing to a data source, etc.

  • If the effect function returns another function (cleanup function), that returned function will be executed when the component is unmounted or before the effect runs again (if the dependencies change).

Here's an example of how you might use useEffect to fetch data from an API:

import React, { useState, useEffect } from 'react';
import axios from 'axios';

const DataFetchingComponent = () => {
  const [data, setData] = useState([]);

  useEffect(() => {
    // Fetch data from an API
    axios.get('https://api.example.com/data')
      .then(response => {
        setData(response.data);
      })
      .catch(error => {
        console.error('Error fetching data:', error);
      });
  }, []); // Run the effect only once after initial render

  return (
    <div>
      <ul>
        {data.map(item => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
};

export default DataFetchingComponent;

In this example, the useEffect hook is used to fetch data from an API after the initial render. The fetched data is stored in the component's state, and the component renders the data as a list. Keep in mind that useEffect is a fundamental hook in React that helps manage side effects in a declarative and predictable way.