What is useEffect in React?

useEffect is a React Hook that lets you perform side effects in function components. Side effects include fetching data, manipulating the DOM, setting up subscriptions, or updating state outside the render scope. It replaces lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

What is the syntax of useEffect?

useEffect(() => {
// Your effect logic here
}, [dependencies]);
  • The first argument is a function that contains the effect logic.
  • The second argument is an optional dependency array that determines when the effect should run.
    • If you omit the dependency array, useEffect runs after every render
    • If you pass empty array [], it will run only once after the initial render like componentDidMount