reactjs / reactjs/react.dev

Confusing explanation in Challenge 2 of React Learn ("You Might Not Need an Effect")

Open Beginner friendly
#8,099 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: typos
Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

Summary

The explanation in Challenge 2 (Cache a calculation without Effects) of You Might Not Need an Effect is confusing and somewhat misleading. The challenge suggests that replacing the useEffect with useMemo (or extracting a NewTodo component) reduces the number of calls to getVisibleTodos(), but this isn’t accurate. The original code already avoids unnecessary recomputations.

Page

https://react.dev/learn/you-might-not-need-an-effect

Details

In the original implementation, getVisibleTodos() is only called when either todos or showActive change. Typing in the input only updates the text state, which isn’t part of the dependency array—so getVisibleTodos() is not called on every keystroke.

The proposed useMemo solution behaves the same way: it recalculates getVisibleTodos() when todos or showActive change, and not when text changes. As a result, it doesn’t reduce the number of calls compared to the original implementation. Console logs confirm this.

The second suggested approach (extracting NewTodo into a separate component) gives the impression that the initial code was calling getVisibleTodos() on every input change, but that’s incorrect. The original code was already optimized since text wasn’t part of the effect’s dependencies.

The documentation even says:

“This approach satisfies the requirements too. When you type into the input, only the text state variable updates. Since the text state variable is in the child NewTodo component, the parent TodoList component won’t get re-rendered. This is why getVisibleTodos() doesn’t get called when you type. (It would still be called if the TodoList re-renders for another reason.)”

However, this explanation is misleading because getVisibleTodos() wasn’t being called at every input in the first place.

In short:

The useMemo version doesn’t reduce calls to getVisibleTodos(), it's exactly the same.

The second version’s explanation implies a problem that didn’t exist in the original code.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the “You Might Not Need an Effect” page and inspect Challenge 2, especially the useMemo and NewTodo explanations. Verify the dependency behavior described in the issue, then revise the wording so it accurately distinguishes the original implementation from the alternatives; done means the explanation no longer implies that the original recalculates on every input change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.