Skip to content

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

@Niteshagarwal01

Description

@Niteshagarwal01

🐌 React Anti-Patterns & Race Conditions in useLearningProgress

Issue Description

The new src/hooks/useLearningProgress.ts hook suffers from two distinct anti-patterns:

  1. Cascading Renders: In the main useEffect checking authentication, it invokes setCompletedNodes([]) and setLoading(false) synchronously when no user is present. This directly violates React guidelines and triggers unnecessary sequential render cycles.
  2. Redundant Optimistic UI: The toggleNode function manually updates local state before sending the network request. Because Firestore's onSnapshot listener triggers instantly upon local writes (latency compensation), the manual local state management creates conflicting state updates and potential race conditions if the network request fails and reverts the state.

Impact

  • Performance: The cascading renders degrade performance and trigger standard linter warnings (react-hooks/set-state-in-effect).
  • Reliability: Overlapping the custom optimistic UI logic with Firestore's native latency compensation creates brittle edge cases and redundant re-renders when toggling progress nodes.

Proposed Fix

  1. Initialize the default state conditionally before the useEffect body to prevent the synchronous state update.
  2. Simplify toggleNode to exclusively perform the setDoc operation. Rely entirely on the existing onSnapshot listener to handle both the immediate optimistic UI update and the confirmed server update automatically.

Suggested Labels

  • bug
  • performance
  • tech-debt

Metadata

Metadata

Labels

gssoc26This is a official GirlScript Summer of Code label.level:intermediateIntermediate level issuestype:bug

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions