DEV Community

Cover image for πŸ’– My First Real Experience with useEffect (Fetching Data the Right Way)
Usama
Usama

Posted on

πŸ’– My First Real Experience with useEffect (Fetching Data the Right Way)

Today was a special day for me. It was a holiday, and instead of wasting time, I finally understood useEffect properly β€” not just how to write it, but why we use it and how it works in real applications.

This wasn’t just theory. Today I learned how to:

Fetch data from an API

Use async / await inside useEffect

Handle errors with try...catch

Show loading and error UI

Understand the component lifecycle in React

And honestly… I’m very happy 😊


πŸ”„ Understanding useEffect in Simple Words

Before today, useEffect felt confusing.
Now I understand it like this:

useEffect runs code after the component is rendered.

In my case, I wanted to fetch movies from the OMDB API when the app loads. That’s a side effect, and React gives us useEffect exactly for this purpose.

When the component mounts:

useEffect runs

Data is fetched

UI updates automatically

This clicked today πŸ’‘


🌐 Fetching Data with async / await

Inside useEffect, I learned that:

You cannot make the effect function itself async

Instead, you create an async function inside it and call it

This pattern finally makes sense now.

The flow in my head is:

  1. Start loading

  2. Fetch data from API

  3. Convert response to JSON

  4. Update state with movies

  5. Stop loading

Simple and clean.


πŸ›‘ Error Handling (Very Important!)

Today I also learned something very practical: APIs can fail.

So I used try...catch to handle problems like:

Network issues

Invalid responses

Movie not found

Instead of crashing the app, I now:

Catch the error

Save the error message in state

Show a friendly error message in the UI

This feels good 😌


⏳ Loading State = Better User Experience

Another big lesson:

Never leave the user guessing.

While data is loading, I show a loading spinner. When something goes wrong, I show a clear error message. When everything works, I show the movie list.

This taught me how conditional rendering works together with useEffect.


♻️ Component Lifecycle (Now I Get It)

Today I finally understood the lifecycle in a practical way:

Mount β†’ component appears β†’ fetch data

Update β†’ state changes β†’ UI updates

Unmount β†’ cleanup (I’ll learn this next πŸ˜‰)

useEffect helped me see this lifecycle instead of memorizing it.


🧠 What I Learned Today (Quick Summary)

Today’s topic gave me confidence because I learned:

πŸ’– useEffect is for side effects

🌐 How to fetch data correctly

⚠️ Real error handling with try...catch

⏳ Loading states matter

♻️ How React components live and update

This was not just code β€” it was understanding.

Top comments (0)