I used to think React components were only for displaying data. But what happens when you need to fetch an API, start a timer, or change a page title?
Today, I’m documenting how to handle these "Side Effects" using the useEffect hook.
Think of it as a remote control for your component. It lets the UI "reach out" to external systems without blocking the main screen. Here is what I’m practicing to keep my apps synchronized:
1) The Dependency Array: This is the control center. I’m learning how an empty array [] makes the code run only once, while adding specific variables makes it run only when they change.
2) Pure Components: I'm focusing on keeping my components "pure"—data goes in, and UI comes out—while letting useEffect handle everything that happens "outside" that flow.
3) The Cleanup: I learned that if I start a timer or a subscription, I have to "turn off the lights" when the component is finished. Using the return pattern prevents memory leaks.
I'm still learning the ropes, but mastering this hook is the key to building real-world, data-driven applications.
Tomorrow, I’m tackling the "Prop Drilling" nightmare with the React Context API!
Quick question: When you first started with React, did you find the dependency array confusing? It definitely took me a minute to get it right!
#CodeWithWajid #ReactJS #WebDevelopment #30DaysOfCode #LearningToCode #BuildingInPublic #Frontend #useEffect
You could've used a simple axios call to store the response into a variable and use it directly.