開源日報每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。

今日推薦開源項目:《ai-group-tabs》
今日推薦英文原文:《Fetch Data in React JS in Modern Way》


開源項目

今日推薦開源項目:《ai-group-tabs》傳送門:項目鏈接

A Chrome extension helps you group your tabs with AI.
非常有創意,結合AI能力實現一些小而精功能
demo 演示鏈接


英文原文

今日推薦英文原文:Fetch Data in React JS in Modern Way

推薦理由:react 項目中fetch 方法獲取數據一般步驟,很常用


Fetch Data in React JS in Modern Way

React JS has become one of the go-to tools for building super cool web apps.

And when you』re creating apps that need to talk to the internet and fetch stuff, you』ve got to know how to do it right.

In this blog, I』m going to show you the new, hip way to fetch data in React using hooks and the fetch API.

Why Fetch Data in React, Anyway?

Before we dive into the modern way of doing things, let』s chat about why it』s so darn important to fetch data in React apps.

React is all about creating those fancy, dynamic web pages that can show you different things based on what you want.

Whether you』re building a simple blog, checking the weather, or running a big online store, data is like the secret sauce that makes your app delicious.

Here are some places where you might need to fetch data in your React app:

  1. Loading Content: You want to show data from a website, a database, or some secret data vault. To do that, you』ve got to fetch the data first and then show it on your page.
  2. User Stuff: Maybe you』ve got users in your app, and you need to fetch their info or make sure they』re who they say they are. Fetching user data is vital for keeping your app safe and personal.
  3. Real-Time Fun: Sometimes, you want to see stuff happen right when it happens — like chat messages popping up or stock prices changing. To make that magic work, you』ve got to fetch data in real-time.
  4. Form Magic: When someone fills out a form on your app, you might want to send that data to a server and then get some new data back. It』s like sending a letter and getting a reply!

Now, let』s talk about the modern way to do all this fetching.

Modern Way of Fetching Data

Back in the day, we used to fetch data using class components and lifecycle methods like componentDidMount and componentDidUpdate.

But now, we've got hooks. React introduced hooks in version 16.8, and they've made our lives easier when it comes to fetching data.

The main hooks we』ll use for this data-fetching party are useState and useEffect.

Let's break down the steps:

Step 1: Import Dependencies

First, make sure to import the necessary dependencies at the top of your component file:

import React, { useState, useEffect } from 'react';

Step 2: Initialize State

Next, initialize state variables to hold the fetched data and manage the loading and error states:

function MyComponent() {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

Step 3: Fetch Data

Inside the component, use the useEffect hook to fetch data when the component mounts:

useEffect(() => {
// Your data fetching logic goes here
fetch('https://api.example.com/data')
.then((response) => response.json())
.then((data) => {
setData(data);
setLoading(false);
})
.catch((error) => {
setError(error);
setLoading(false);
});
}, []); // The empty array [] ensures this effect runs only once when the component mounts

Step 4: Render Data

Finally, render the fetched data in your component』s JSX based on the loading and error states:

return (

{loading ? (

Loading...

) : error ? (

Error: {error.message}

) : (

    {data.map((item) => (

  • {item.name}
  • ))}

)}

);
}

That』s it! With these steps, you can fetch and display data in a modern React component.

This approach allows you to manage data fetching and component rendering in a more organized and efficient manner.

Conclusion

Fetching data is a crucial part of building dynamic React applications, and React provides modern tools like hooks to simplify the process.

By using useState and useEffect, you can manage data fetching, loading, and error handling in a clean and efficient way.

This modern approach ensures that your React components are responsive and provide a seamless user experience, making your web applications more robust and user-friendly.

So, embrace the modern way of fetching data in React and build amazing, data-driven applications!


下載開源日報APP:https://openingsource.org/2579/
加入我們:https://openingsource.org/about/join/
關注我們:https://openingsource.org/about/love/