Last active
March 30, 2021 14:50
-
-
Save herberthk/15bda3ae6b80ea80beeea1e0a881bddb to your computer and use it in GitHub Desktop.
Contra-challenge
This file contains hidden or 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
/** | |
* 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> | |
</> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
made it public