Skip to content

Instantly share code, notes, and snippets.

@herberthk
Last active March 30, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herberthk/15bda3ae6b80ea80beeea1e0a881bddb to your computer and use it in GitHub Desktop.
Save herberthk/15bda3ae6b80ea80beeea1e0a881bddb to your computer and use it in GitHub Desktop.
Contra-challenge
/**
* First issue was missing fallback prop on suspense which is required
*
* The second issuse was useEffect hook dependences of SuspensefulUserProfile component only required userId such that it can re-render if userId prop changes
*
* The third issuse was Suspense component was used on sub component not on main component
*/
import { Suspense, useState, useEffect } from 'react';
const SuspensefulUserProfile = ({ userId }) => {
const [data, setData] = useState({});
useEffect(() => {
setData(data)
fetchUserProfile(userId).then((profile) => setData(profile));
}, [userId])
return (<UserProfile data={data} />);
};
const UserProfile = ({ data }) => {
return (
<>
<h1>{data.name}</h1>
<h2>{data.email}</h2>
</>
);
};
const UserProfileList = () => (
<>
<Suspense fallback={<h1>Loadinng user profile 2</h1>}>
<SuspensefulUserProfile userId={1} />
</Suspense>
<Suspense fallback={<h1>Loadinng user profile 2</h1>}>
<SuspensefulUserProfile userId={2} />
</Suspense>
<Suspense fallback={<h1>Loadinng user profile 2</h1>}>
<SuspensefulUserProfile userId={2} />
</Suspense>
</>
);
@herberthk
Copy link
Author

made it public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment