Kritim Yantra
Jul 09, 2025
Imagine you're browsing your favorite recipe site. You click on “Quick Pasta Recipes” — and BOOM! — new recipes load without refreshing the page. ✨ No flicker, no reload. Just magic.
But wait… what kind of sorcery is this?
Well, behind the scenes, tools like Axios, useState
, and useEffect
in React.js are making it happen — and guess what? You can learn how to do it too. 🙌
Whether you're building a weather app 🌦️, a movie list 🎬, or your own startup's front end — understanding how to fetch and display data in React is a skill you’ll use again and again.
Let’s break it down together — nice and easy, like chatting over coffee ☕.
By the end, you’ll be able to:
useState
useEffect
Let’s go! 🎯
Think of Axios as a delivery service. You send it a request ("Get me the latest Pokémon!"), and it brings back the data neatly packaged. It’s a popular alternative to the browser’s built-in fetch()
method — and way easier to use.
Why Axios?
📦 Install it with:
npm install axios
useState
– Your App’s MemoryReact doesn’t have a “database” built-in, so we use state to remember things. useState
is a hook (a special React function) that lets you store and update values inside components.
Example:
const [count, setCount] = useState(0);
count
: your current valuesetCount
: a function to update ituseEffect
– Side Effect SupervisoruseEffect
is how you tell React: “Hey, do this after the page has rendered.” Perfect for fetching data when the component first loads.
Think of it like ordering coffee when you walk into a café: the page loads (you enter), and then a request is made (your coffee order).
Example:
useEffect(() => {
// Do something here (like fetch data)
}, []);
That empty array []
means "do this once, when the component loads."
We'll create a small app that fetches a list of users from a free API and displays them.
import React, { useState, useEffect } from 'react';
import axios from 'axios';
function UserList() {
const [users, setUsers] = useState([]); // Store users
const [loading, setLoading] = useState(true); // Track loading
const [error, setError] = useState(null); // Handle errors
useEffect(() => {
axios.get('https://jsonplaceholder.typicode.com/users')
.then(response => {
setUsers(response.data); // Save users
setLoading(false); // Turn off loading
})
.catch(err => {
setError(err.message); // Save error message
setLoading(false);
});
}, []);
return (
<div>
<h1>📋 User List</h1>
{loading && <p>Loading users...</p>}
{error && <p>Error: {error}</p>}
<ul>
{users.map(user => (
<li key={user.id}>
👤 {user.name} – {user.email}
</li>
))}
</ul>
</div>
);
}
export default UserList;
Concept | What It Does | Real-Life Metaphor |
---|---|---|
Axios | Fetches data from the internet | A food delivery service 🍔 |
useState |
Stores dynamic values | Your phone’s memory 🧠 |
useEffect |
Triggers code after rendering | Making a call after walking into a room 📞 |
💡 Tip 1: Always handle loading and error states. It makes your app user-friendly and professional.
💡 Tip 2: The empty array []
in useEffect
means “run once.” Add values in the array if you want it to re-run when they change.
💡 Tip 3: Don’t overthink it — start small, break things, and fix them. It’s the React way. 🔧
Learning how to fetch and manage data in React opens the door to real-world app development. Whether it's social feeds, product listings, or weather data — it’s all about connecting your front end to the outside world. 🌎
Now that you’ve met Axios, useState
, and useEffect
, you’ve got some of React’s most essential power tools in your kit. 🧰
What kind of data would you love to display in a React app?
Let us know in the comments below! 💬👇
fetch()
instead of Axios?Yes, fetch()
is built into the browser and works fine! Axios just adds some helpful features, like automatic JSON conversion and better error handling.
[]
in useEffect
?Without it, the effect will run every time your component re-renders, which might cause infinite loops if you're updating state inside the effect.
useState([])
and not useState({})
?Because the data from the API is an array of users, not a single object. We match our state structure to the expected data format.
✅ Try it out now! Create your own React component that fetches dog breeds, countries, or GitHub profiles. Once you try it hands-on, it’ll really start to click.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google