AbhayVel / yogesh911

0 stars 0 forks source link

What is the use of useEffect React Hooks? #45

Open YogeshG85 opened 1 year ago

YogeshG85 commented 1 year ago

useEffect is a React Hook that allows you to handle side effects in a functional component. Side effects can be anything that affects the state of your application outside of your component, such as fetching data from an API, modifying the DOM, or setting up event listeners.

useEffect allows you to perform these side effects and manage their lifecycle in a declarative way. It takes two arguments: a function that performs the side effect, and an optional array of dependencies that determine when the effect should be run.

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

function MyComponent() { const [data, setData] = useState(null);

useEffect(() => { async function fetchData() { const response = await fetch('https://api.example.com/data'); const data = await response.json(); setData(data); } fetchData(); }, []);

return (

{data ? (
    {data.map((item) => (
  • {item.name}
  • ))}
) : (

Loading...

)}

); }`