Last active
January 2, 2021 03:41
-
-
Save gcjbr/1491c5693172fb2438ad3c52dfdd87f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* There are 3 key problems with the React code below. Can you find them? | |
* Assume fetchUserProfile exists elsewhere. | |
*/ | |
import React, { Suspense, useState, useEffect } from "react"; | |
const SuspensefulUserProfile = ({ userId }) => { | |
const [data, setData] = useState({}); | |
useEffect(() => { | |
// Suspense won't work with Promises | |
const profile = fetchUserProfile(userId).read(); | |
setData(profile); | |
}, [userId]); | |
// Remove setData | |
// Add fallback | |
return ( | |
<Suspense fallback={<div>Loading ...</div>}> | |
<UserProfile data={data} /> | |
</Suspense> | |
); | |
}; | |
const UserProfile = ({ data }) => { | |
return ( | |
<> | |
<h1>{data.name}</h1> | |
<h2>{data.email}</h2> | |
</> | |
); | |
}; | |
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