Skip to content

React pr#21

Open
jorisstander wants to merge 2 commits intomasterfrom
react-pr
Open

React pr#21
jorisstander wants to merge 2 commits intomasterfrom
react-pr

Conversation

@jorisstander
Copy link
Copy Markdown

  • Fetches user data asynchronously from an API using a provided user ID and auth token from context.
  • Displays user information (email, location) and user statistics (posts, followers, following) via separate subcomponents (UserInfo and UserStats).
  • Shows loading and error states during data fetching.
  • Uses React Router v6 to provide navigation links inside the dashboard.
  • Provides a simple AuthContext to supply userId and token for authentication purposes.

fetchUserData(userId, token)
.then(data => {
setUserData(data);
setLoading(false);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idealiter wil je loading states niet in een .then hebben. Je wil in een .then alleen logica hebben zitten die moeten afhangen van Promise resolve state. Ik zou setLoading(false) gebruiken in een .finally() waar je alleen dingen wil doen die je sowieso al wilde doen onafhankelijk van wat de Promise teruggeeft.

})
.catch(err => {
setError('Failed to fetch user data');
setLoading(false);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hetzelfde verhaal hier als de vorige comment. Dit zou ik uit de catch gooien. Het is sowieso dubbelop dat je setLoading in zowel de .then als in de .catch gebruikt. Logischer is een .finally() hier waar je de loading state terug naar false zet.

if (loading) return <div>Loading user data...</div>;
if (error) return <div>Error: {error}</div>;

return (
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wat ik hier mis: stel dat loading false is en error niks bijzonders teruggeeft, maar userData alsnog null of leeg is. Dan loop je het risico dat die user profile niet correct rendert. Ik zou hier nog een derde guard toevoegen: if(!userData) { }. Puur om zeker te zijn.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants