Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Francisldn
Francisldn

Posted on

     

React Data Fetching Patterns - Part I

In React,data fetching from an external API typically involves a few steps, as below:

  1. send a fetch request
  2. await for response
  3. receive the requested data or error

In this article, we will first discuss how data fetching is traditionally done in React usinguseState-useEffect hooks. We will then proceed to discuss other data fetching patterns, utilisingSuspense - an API made available in React v18 (no longer experimental), as follows:

Clickhere to access the Github repo for the code examples discussed below.

How data fetching is traditionally done

In React, we traditionally perform data fetching usinguseState-useEffect hooks. Within a React component, a state is created usinguseState hook to store data.useEffect hook is executed as the component first mounts or page first renders. The data fetching logic in the useEffect hook will run and data will be stored as a state.

Let's go through the code:

  • users state is declared withuseState, together with loading and error states
  • WithinuseEffect,getUsers function will execute on component mounts, and the loading state will set to true while waiting for data to be retrieved
  • On successful data retrieval, the loading and error states will set to false, andusers state will contain the retrieved data, ready for UI rendering.
export default function CardList() {    const [users, setUsers] = useState<User[]>([])    const [isLoading, setLoading] = useState(false)    const [isError, setError] = useState(false)    useEffect(() => {         setLoading(true)          getUsers(apiURL)            .then(({results:data}) => {                setUsers(                data.map((user:UserApi) => {                    return {                        id: user.id.value,                        firstName: user.name.first,                        lastName: user.name.last,                        username: user.login.username,                        email: user.email,                    }}))                })            .catch(() => setError(true))            .finally(() => setLoading(false))    },[])  return (    <div className="grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 justify-center grid-cols-1 mt-8">        {isLoading         ? <Loading />        : isError         ? <ErrorFallBack />        : users.map((user:User) => (<CardItem key={user.id} user={user}/>))        }    </div>  )}
Enter fullscreen modeExit fullscreen mode

Downsides

Prop-drilling
While this data fetching pattern works fine for a local component, if there were multiple components that require access to the same set or a subset of the data, then we will need to pass the props through multiple child components, which could lead toproblems of prop-drilling. For a larger app, prop-drilling through multiple layers of components can make the app unwieldy and hard to manage.

To overcome the issues outlined above, let's proceed to look at a few other data fetching methods, as below.

To be continued:

If you like the content, please hit the like button so that it can reach more people.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jim462 profile image
Jim
  • Joined
• Edited on• Edited

"To be continued" wtf? It's 2 years later. Where is it?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Freelance Frontend/Full Stack Developer
  • Joined

More fromFrancisldn

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp