A React component's primary job during rendering is pure: calculating the Virtual DOM representation for a given set of props and state.
However, modern applications frequently need to perform operations that reach outside the component's render scope:
document.title or reading geolocation)setTimeout, setInterval)These operations are known as Side Effects (or simply "effects"). In React functional components, side effects are managed using the useEffect hook.
useEffectThe useEffect hook accepts two arguments:
useEffectWhen fetching data on initial component mount:
Note: The callback function passed to useEffect cannot be marked async directly (e.g. useEffect(async () => ...) is forbidden) because an async function returns a Promise, whereas React expects useEffect to return either a cleanup function or nothing!
useEffect callback function execute relative to the browser painting the screen?useEffect(async () => { ... }, [])?[] signify to useEffect?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Rules of React Hooks | The Dependency Array and Execution Lifecycle |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.