Skip to content

#1775 caused useQueries to return unstable values between render cycles #1905

@szszoke

Description

@szszoke

Describe the bug
The array returned by useQueries is not stable between render cycles which makes it unusable for anything down the line that uses shallow equality to determine if something had changed.

Small snippet to illustrate the bug

const responses = useQueries(preparedQueries);

console.log("changed?");
useEffect(() => console.log("changed!"), [responses]);

Console output

changed?
changed!
changed?
changed!
...

Expected behavior
The return value of useQueries is stable between render cycles as long as the queries did not change. The returned value can be used as a dependency for hooks like useMemo/useEffect which use shallow equality checks to determine if a dependency had changed since the last render

Desktop (please complete the following information):

  • OS: Linux
  • Browser: Firefox
  • Version: 86

Additional context
The new behavior was introduced via #1775.

Relevant code lines:

https://github.com/tannerlinsley/react-query/blob/f2137dc4e4553256c4ebc1891b548fe35efe9231/src/react/useQueries.ts#L29

https://github.com/tannerlinsley/react-query/blob/f2137dc4e4553256c4ebc1891b548fe35efe9231/src/core/queriesObserver.ts#L67

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions