Skip to content

Conversation

@PaulyBearCoding
Copy link

Fixes #8097

Problem

In the "Chains of computations" section, the refactored example uses an incorrect condition that changes the game logic from the original:

// Current (incorrect)
if (goldCardCount <= 3) {
  setGoldCardCount(goldCardCount + 1);
} else {
  // advance round
}

This allows 5 gold cards before advancing the round, which differs from the original Effect-based example that advances after 4 gold cards.

Solution

Changed the condition from <= to <:

// Fixed
if (goldCardCount < 3) {
  setGoldCardCount(goldCardCount + 1);
} else {
  // advance round
}

Why This Fix is Correct

The original "bad" example increments first, then checks:

useEffect(() => {
  setGoldCardCount(c => c + 1);  // 0→1, 1→2, 2→3, 3→4
}, [card]);

useEffect(() => {
  if (goldCardCount > 3) {  // Triggers when count reaches 4
    setRound(r => r + 1);
    setGoldCardCount(0);
  }
}, [goldCardCount]);

This advances after 4 cards (when count reaches 4).

The fixed example checks before incrementing:

  • With <= 3: Allows counts 0,1,2,3 to increment → 4 values → advances on 5th card ❌
  • With < 3: Allows counts 0,1,2 to increment → 3 values → advances on 4th card ✅

Testing

Verified the fix by:

  1. Tracing execution logic step-by-step
  2. Building the docs site locally and confirming correct rendering
  3. Checking that only the intended line was modified

Changes

  • File: src/content/learn/you-might-not-need-an-effect.md
  • Line: 440
  • Change: goldCardCount <= 3goldCardCount < 3

Fixes reactjs#8097

The refactored example in the "Chains of computations" section uses
an incorrect condition that changes the game logic from the original.

The original Effect-based code advances the round after 4 gold cards:
- Increments first (0→1, 1→2, 2→3, 3→4)
- Then checks `goldCardCount > 3` (true when count is 4)

The refactored code with `goldCardCount <= 3` allows 5 gold cards:
- Checks before incrementing
- Allows counts 0, 1, 2, 3 to increment (4 values)
- Advances on the 5th card (when count is 4)

This fix changes the condition to `goldCardCount < 3`:
- Allows counts 0, 1, 2 to increment (3 values)
- Advances on the 4th card (when count is 3)
- Matches the original behavior

Verified by tracing execution logic and building the docs site locally.
@meta-cla
Copy link

meta-cla bot commented Oct 29, 2025

Hi @PaulyBearCoding!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@github-actions
Copy link

Size changes

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@meta-cla meta-cla bot added the CLA Signed label Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Typo]: Incorrect condition in Chains of computations section

1 participant