reactjs / reactjs/react.dev

improve documentation for useEffect hook

Open
#1,853 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

useEffect takes an array of values that may cause the effect to rerun if they "change".

Two comments: First, what is "change?" From this issue I learned that React uses Object.is to that end, but the documentation for conditionally firing an effect does not state this.

Second, here's what the documentation says:

To implement this, pass a second argument to useEffect that is the array of values that the effect depends on. Our updated example now looks like this:

useEffect(
  () => {
    const subscription = props.source.subscribe();
    return () => {
      subscription.unsubscribe();
    };
  },
  [props.source],
);

Now the subscription will only be recreated when props.source changes.

The wording "when props.source changes is confusing and ambiguous (in my opinion).
For instance, if props.source refers to an object with property x, and some other code, or even code in the same scope, does props.source.x++, then did props.source change?

I think - correct me if I'm wrong - or better, update the documentation - I think that what is meant if the value passed at one call to useEffect() is different from the value passed at another call that happens during a subsequent call to the function in which the call to useEffect is embedded, then the value in this position of the array is considered to have changed. Two values v1 and v2 are considered different iff Object.is(v1, v2) returns false. React does not try to somehow "watch" any objects that are being passed to useEffect's dependency array.
I think this follows from thinking about how useEffect could possibly be implemented, but it would help the reader to state it clearly.
It would also help if the notion of "change" is more clearly defined as "change across calls to the function in which the call to useEffect is embedded, " in my opinion.

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

Start by reading the linked “Conditionally firing an effect” section and issue #15154 for the existing explanation of dependency comparison. Clarify that React compares dependency values across renders with Object.is and does not watch mutations inside referenced objects; done means the useEffect documentation removes the ambiguity around “changes.”

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
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.