Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Angular query] Queries with enabled: false have their fields not behaving as expected #8545

Open
JulienLecoq opened this issue Jan 17, 2025 · 2 comments
Assignees

Comments

@JulienLecoq
Copy link

Describe the bug

When setting a query with enabled: false and calling query.refetch() in the ngOnInit life cycle callback, many of the fields of the query behave abnormally.

For exemple, the isFetching signal and isLoading signal will have their value set to false even if the query is fetching.

The isFetching and isLoading signals will be set to true only on a retry of the query.

Also, if the query results in an error, the next time the ngOnInit calls query.refetch() the query's error will not be reset to null, it will only be set to null if there is a retry of the query.

I imagine that there are some other weird things happening not listed here.

Your minimal, reproducible example

https://codesandbox.io/p/devbox/angular-query-enabled-bug-fnslsy

Steps to reproduce

Click on the reproduction URL and I guess it should be sufficient to see the issue. I'm not sure how codesandbox work :/

Expected behavior

The isFetching and isLoading signals should be set to true right after calling .refetch. During a refetch, the error signal should be set to null.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Safari

Tanstack Query adapter

angular-query

TanStack Query version

v5.64.1

TypeScript version

v5.7.3

Additional context

No response

@arnoud-dv
Copy link
Collaborator

arnoud-dv commented Jan 18, 2025

Thank you for reporting this and the repro.

This happens because ngOnInit is too soon in the lifecycle. Refetch is called on the query before its effect is executed. This effect subscribes to the observer. This is done asynchronously so that input.required() signals can be used seamlessly in the query options without having to be bothered by component lifecycle.

Probably the best option is to use the query declaratively and set enabled to true using a signal.

Also, ngOnInit - or probably any lifecycle hook is not needed anymore in modern Angular, see this great talk by Matthieu Riegler.

Imperative alternatives that do work

  constructor() {
    afterNextRender(() => {
      this.query.refetch();
    })
  }
  refetchEffect = effect(() => {
    this.query.refetch();
  })

  // or in the constructor:
  constructor() {
    effect(() => {
      this.query.refetch();
    })
  }

I'll add this to the documentation I'm writing about how to work with Angular reactivity and TanStack Query.

@JulienLecoq
Copy link
Author

Thanks for the great explanation! This shouldn't affect me but I discovered it while working on my wrapper around Angular query so I preferred to report it to be sure 👌🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants