Skip to content

Instantly share code, notes, and snippets.

@forrestwilkins
Created November 17, 2021 21:21
Show Gist options
  • Save forrestwilkins/f3ef75e89bc24b3644bdbf8744112e6f to your computer and use it in GitHub Desktop.
Save forrestwilkins/f3ef75e89bc24b3644bdbf8744112e6f to your computer and use it in GitHub Desktop.
import { Suspense, useState, useEffect } from "react";
const fetchUserProfile = (userId) => {
return new Promise((resolve) => {
resolve({
name: `Forrest ${userId}`,
email: `forrest${userId}@gmail.com`,
});
});
};
const UserProfile = ({ data }) => {
return (
<>
<h1>{data.name}</h1>
<h2>{data.email}</h2>
</>
);
};
const SuspensefulUserProfile = ({ userId }) => {
const [data, setData] = useState({});
useEffect(() => {
fetchUserProfile(userId).then((profile) => setData(profile));
}, [userId, setData]);
return (
<Suspense>
<UserProfile data={data} />
</Suspense>
);
};
const UserProfileList = () => (
<>
<SuspensefulUserProfile userId={1} />
<SuspensefulUserProfile userId={2} />
<SuspensefulUserProfile userId={3} />
</>
);
export default UserProfileList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment