useEffect
hook is to handle side effects in functional components. Side effects refer to any actions that are performed by a component that have effects outside of the component itself, such as fetching data from an API, subscribing to event listeners, manipulating the DOM, or updating document titles. The useEffect
hook allows you to perform these side effects in a controlled and declarative way within a functional component.useEffect
hook, you pass a callback function as the first argument. This callback function contains the side effect logic. The effect is executed immediately after the component has rendered for the first time, and subsequently after each update. The effect’s logic can interact with the component by accessing its state, props, or other variables, and it can perform actions like modifying the state, making API requests, subscribing/unsubscribing to event listeners, and more.useEffect
hook. This return value is used to clean up the effect or perform additional actions when the component unmounts or before the effect is re-run. If the effect needs to perform any cleanup, such as cancelling subscriptions or removing event listeners, you can return a cleanup function from the effect’s logic function. This cleanup function will be executed when the component unmounts or before the effect is re-run. Additionally, if the effect depends on certain values or props, returning a function that handles the cleanup helps to ensure that the cleanup is performed properly when those dependencies change or the component unmounts.Keep these pages handy - they answer questions that show up regularly for this lab.