In React, components focus on UI, but what if you need to fetch data, update the DOM, or work with APIs? That’s where useEffect() comes in!
-What is useEffect? – It’s a React Hook that lets you perform
side effects in functional components, like API calls or
updating the document title.
-Runs After Render – Unlike normal code inside a component,
useEffect runs after the UI updates, keeping things smooth.
-Dependency Array Magic – You can control when useEffect runs:
-Empty []
→ Runs only once (like componentDidMount
)
-With dependencies [state
] → Runs when state changes
-No array → Runs after every render
-Cleanup Function – Prevent memory leaks by returning a function inside useEffect, useful for removing event listeners or timers.
-🔥 Final Thought: useEffect
makes React components smarter by handling background tasks efficiently.
Top comments (0)