Skip to content

[BUG] setState in useList queryOptions select causes infinite loop #7022

@trantam246

Description

@trantam246

Describe the bug

When using the select function in queryOptions of the useList hook, it causes an infinite re-rendering loop. The component continuously re-fetches data and never stabilizes, making the application unusable.

The issue occurs specifically when the select function is used to transform or filter the data returned from the API call within the useList hook's query options.

Steps To Reproduce

  1. Create a component that uses useList hook
  2. Add queryOptions with a select function
  3. Call setState or any state setter inside the select function
  4. Observe infinite re-rendering in browser dev tools
    const { data } = useList({
    resource: "users",
    queryOptions: {
    select: (data) => {
    // This causes infinite loop
    setSomeState(data.items);
    return data;
    }
    }
    });
Image

Expected behavior

The select function should be able to handle side effects like setState calls without causing infinite re-renders. The query should execute once and allow state updates based on the transformed data without triggering additional query executions.

Packages

"@ant-design/icons": "^5.5.1",
"@refinedev/antd": "^6.0.1",
"@refinedev/cli": "^2.16.48",
"@refinedev/core": "^5.0.1",
"@refinedev/devtools": "^2.0.1",
"@refinedev/react-router": "^2.0.0",

Additional Context

This issue commonly occurs when developers need to update component state based on the transformed query data. The current behavior makes it impossible to perform side effects within the select function, limiting its usefulness for data transformation scenarios that require state updates.

Metadata

Metadata

Assignees

No one assigned

    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