-
Notifications
You must be signed in to change notification settings - Fork 1
Reset per-post social state on post switch to prevent leakage of likes and comments #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,6 +248,16 @@ export const Profile = () => { | |
| } | ||
| }, [socialData]); | ||
|
|
||
| // CRITICAL: Clear social states when switching posts to prevent showing wrong comments/likes | ||
| useEffect(() => { | ||
| // Reset to empty states when selectedId changes | ||
| setLikesCount(0); | ||
| setIsLiked(false); | ||
| setComments([]); | ||
| setNewComment(''); | ||
| setIsSubmittingComment(false); | ||
| }, [selectedId]); | ||
|
Comment on lines
+252
to
+259
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Race condition: Reset effect overwrites cached social data when switching posts Same issue as in
This causes incorrect display of likes count (showing 0 instead of actual count), The fix should reorder the effects or use a different approach to handle the transition state. Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| const handleToggleLike = async () => { | ||
| if (!user || !selectedId) return; | ||
| const prevLiked = isLiked; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Race condition: Reset effect overwrites cached social data when switching posts
In both
Karya.tsxandProfile.tsx, the newly addeduseEffectthat resets social states (likes, comments, etc.) whenselectedIdchanges runs AFTER the effect that syncs fromsocialData. This causes a race condition:selectedIdchanges to a previously viewed post, React Query immediately returns cachedsocialDatasocialDatasync effect (lines 347-353 in Karya.tsx, 243-249 in Profile.tsx) runs and sets the correct valuesThis results in:
The fix should either:
selectedIdand only reset when actually switching, orWas this helpful? React with 👍 or 👎 to provide feedback.