Skip to content

Why useEffect triggers twice after the API response is cached? #5538

Closed
@TkDodo

Description

@TkDodo

Discussed in #5537

Originally posted by dev-rish June 5, 2023
Describe the Bug
After switching between 1 & 2 for couple of times (so that react query caches data for each of them), the useEffect still triggers twice.
The expected behavior is seen till v3.8.3 i.e. useEffect only triggers once. Once react query is updated to v3.9.0 every click on 1 or 2 causes useEffect to trigger twice.

Is there a way to prevent the second trigger?

I found related issues but still wanted some more info. if possible.

Minimal, reproducible example
v3: https://codesandbox.io/s/optimistic-firefly-1tk7i5
v4: https://codesandbox.io/s/sad-scott-cidk6s?file=/src/App.js

Steps to reproduce
Click in 1 > Click on 2 > Click in 1 > Click on 2

Expected behavior
useEffect to trigger only once when id is switched.

How often does this bug happen?
Always

Note:

  • Seems to be select related, because it works as expected if select is removed
  • This is not strict mode related

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions