Skip to content

Instantly share code, notes, and snippets.

@gcjbr
Last active January 2, 2021 03:41
Show Gist options
  • Save gcjbr/1491c5693172fb2438ad3c52dfdd87f0 to your computer and use it in GitHub Desktop.
Save gcjbr/1491c5693172fb2438ad3c52dfdd87f0 to your computer and use it in GitHub Desktop.
/*
* 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