useEffect in React JSWelcome to our comprehensive guide on data fetching with useEffect in React JS! In this tutorial, we'll explore how to fetch and manage data in your React applications. By the end of this lesson, you'll have a solid understanding of useEffect, and you'll be able to apply these concepts to your own projects. šÆ
useEffect is a React Hook that lets you perform side effects in your functional components. It's a powerful tool for fetching data, handling subscriptions, and more. In this lesson, we'll focus on using useEffect for data fetching.
Before diving into data fetching, make sure you have a basic understanding of the following concepts:
By the end of this tutorial, you'll know how to:
Let's start by creating a new React project using Create React App:
npx create-react-app data-fetching-demo
cd data-fetching-demoThe project structure looks like this:
data-fetching-demo
āāā node_modules
āāā public
āāā src
ā āāā App.css
ā āāā App.js
ā āāā App.test.js
ā āāā index.css
ā āāā index.js
ā āāā logo.svg
āāā package.json
To start the project, run the following command in your terminal:
npm startuseEffectNow let's dive into the main topic: fetching data with useEffect.
First, we'll create a new component called PostList that will display a list of posts. We'll use useEffect to fetch the data and update the component when it changes.
Inside the src folder, create a new file called PostList.js:
touch src/PostList.jsReplace its contents with the following code:
import React, { useState, useEffect } from 'react';
function PostList() {
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
// Fetch data and manage loading state
}, []);
return (
<div>
{loading && <p>Loading...</p>}
{error && <p>Error: {error}</p>}
{posts.map((post, index) => (
<div key={index}>
<h2>{post.title}</h2>
<p>{post.body}</p>
</div>
))}
</div>
);
}
export default PostList;In this code, we've defined three state variables: posts, loading, and error. The useEffect hook is empty for now, but we'll fill it in soon.
Now we'll use useEffect to fetch data from the JSONPlaceholder API. We'll make an API call to fetch a list of posts and update the component with the response.
Replace the contents of the useEffect hook in the PostList component with the following code:
useEffect(() => {
const fetchPosts = async () => {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await response.json();
setPosts(data);
setLoading(false);
} catch (error) {
setError(error);
setLoading(false);
}
};
fetchPosts();
}, []);This code defines an asynchronous function called fetchPosts that fetches the data from the API and updates the component's state. It also handles errors by setting the error state variable.
Now we'll update the App component to include the PostList component.
Replace the contents of the App.js file with the following code:
import React from 'react';
import './App.css';
import PostList from './PostList';
function App() {
return (
<div className="App">
<PostList />
</div>
);
}
export default App;With this change, the PostList component will be rendered inside the App component, and data will be fetched and displayed.
To see the data fetching in action, run the following command in your terminal:
npm startYou should now see the list of posts being displayed in your browser.
To improve performance, we can implement a simple cache for the data. This way, we can avoid unnecessary API calls when the component is updated.
Update the useEffect hook in the PostList component with the following code:
useEffect(() => {
let isMounted = true;
const fetchPosts = async () => {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await response.json();
if (isMounted) {
setPosts(data);
setLoading(false);
}
} catch (error) {
setError(error);
setLoading(false);
}
};
fetchPosts();
// Cleanup function
return () => {
isMounted = false;
};
}, []);This code adds a let isMounted = true variable that tracks whether the component is currently mounted. The fetchPosts function is updated to only set the data and update the loading state if isMounted is true.
The cleanup function is added at the end of the useEffect hook to set isMounted to false when the component unmounts.
You've now learned how to fetch data with useEffect in React JS! You can apply these concepts to fetch data from various APIs and update your components when the data changes.
Remember to handle errors gracefully and manage the loading state to provide a smooth user experience.
š Note: Answers can be found in the comments within the code.
What does the `useEffect` hook do in React JS?
What does the `let isMounted = true` variable do in the `useEffect` hook?
What does the cleanup function do in the `useEffect` hook?