Skip to content

despite setting the enabled option to false, useQuery still initiates a fetch #1196

@aastronaut

Description

@aastronaut

Describe the bug
After an update to 2.23.1 or 3.2.0-beta.3 I noticed that passing the option { enabled: false } to useQuery will still initiate a request when the component mounts. Where this flag was enough to prevent the hook from causing a request on mount in an earlier version (2.5.13 in my case), now I would need to create a separate component and mount it conditionally in order to regain the same wanted behavior, which would result in a lot of boilerplate.

To Reproduce
(on request if unclear)

Expected behavior
I would expect the option { enabled: false } to disable fetching completely, as that would prevent the creation of an conditionally mounted component. However, the expected behavior is unclear to me due to mismatching descriptions in docs and source code:

According to the docs automatically running is mentioned:

enabled: Boolean | unknown
    Set this to false to disable this query from automatically running.
    Actually it can be anything that will pass a boolean condition. See Dependent Queries for more information.

According to the source code automatic refetching is mentioned:

/**
 * Set this to `false` to disable automatic refetching when the query mounts or changes query keys.
 * To refetch the query, use the `refetch` method returned from the `useQuery` instance.
 * Defaults to `true`.
 */
enabled?: boolean | unknown

A clear and concise description of what you expected to happen.
Please either adapt the docs to mention that only refetching is controlled with this option, or a fix is needed for the hook to control also the fetching behavior when a component mounts.

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