Skip to content

Instantly share code, notes, and snippets.

@foyez
Created December 7, 2020 15:27
Show Gist options
  • Select an option

  • Save foyez/d9c9695537395d74e179fa55d2374312 to your computer and use it in GitHub Desktop.

Select an option

Save foyez/d9c9695537395d74e179fa55d2374312 to your computer and use it in GitHub Desktop.
import React from 'react'
const UserProfile = ({ data }) => {
return (
<>
<h1>{data.name}</h1>
<h2>{data.email}</h2>
</>
)
}
export default UserProfile
import React, { Suspense, useState, useEffect } from 'react'
const UserProfile = React.lazy(() => import('./UserProfile'))
const SuspensefulUserProfile = ({ userId }) => {
const [data, setData] = useState({})
useEffect(() => {
const fetchData = async () => {
const profile = await fetchUserProfile(userId)
setData(profile)
}
fetchData()
}, [userId])
return (
<Suspense fallback={<p>loading...</p>}>
<UserProfile data={data} />
</Suspense>
)
}
const UserProfileList = () => (
<>
<SuspensefulUserProfile userId={1} />
<SuspensefulUserProfile userId={2} />
<SuspensefulUserProfile userId={3} />
</>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment