Skip to content

fix: resolve cascading renders and optimistic UI race conditions in u…#609

Merged
Aditya948351 merged 2 commits into
devpathindcommunity-india:masterfrom
Niteshagarwal01:fix/use-learning-progress-antipatterns
Jun 14, 2026
Merged

fix: resolve cascading renders and optimistic UI race conditions in u…#609
Aditya948351 merged 2 commits into
devpathindcommunity-india:masterfrom
Niteshagarwal01:fix/use-learning-progress-antipatterns

Conversation

@Niteshagarwal01

Copy link
Copy Markdown
Contributor

PR: Fix React Anti-Patterns in useLearningProgress

Closes #606

Title

fix: resolve cascading renders and optimistic UI race conditions in useLearningProgress

Description

Issue:

  1. Cascading Renders: The hook previously called setCompletedNodes([]) and setLoading(false) synchronously within the useEffect body when an unauthenticated user was detected. This violated React guidelines and caused the react-hooks/set-state-in-effect linter error.
  2. Race Conditions: The toggleNode function employed manual local state updates (Optimistic UI) which conflicted with the onSnapshot listener's built-in latency compensation. This caused duplicate state triggers and potential race conditions if a network update failed.

Fix:

  • Derived the fallback empty state dynamically using standard JS ternary operators based on the user context variable instead of firing synchronous state mutations inside the useEffect.
  • Removed setLoading(true) from the hook's effect body (as loading initializes to true globally, and the cleanup routine or subsequent onSnapshot adequately manages its transitions).
  • Stripped the manual optimistic UI updates from toggleNode, completely deferring to Firestore's native onSnapshot latency compensation which safely handles instantaneous local updates out-of-the-box.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactoring (performance improvements)

Testing

  • Ran npx eslint src/hooks/useLearningProgress.ts locally and verified that the react-hooks/set-state-in-effect rule is no longer violated.
  • TypeScript compilation passes flawlessly.

@github-actions github-actions Bot added gssoc26 This is a official GirlScript Summer of Code label. level:intermediate Intermediate level issues type:bug labels Jun 13, 2026
@Aditya948351 Aditya948351 merged commit fd20f3e into devpathindcommunity-india:master Jun 14, 2026
2 checks passed
@Aditya948351 Aditya948351 added the gssoc:approved give 50+ base points label Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved give 50+ base points gssoc26 This is a official GirlScript Summer of Code label. level:intermediate Intermediate level issues type:bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] React Anti-Patterns & Race Conditions in useLearningProgress

2 participants