开源日报每天推荐一个 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/