Git Product home page Git Product logo

Comments (2)

TkDodo avatar TkDodo commented on May 28, 2024 1

the options do update on the query, but not on the retry. Let me explain:

when a query starts to run, we create an internal retryer and pass options from the query to it:

retry: context.options.retry,
retryDelay: context.options.retryDelay,
networkMode: context.options.networkMode,

Then, that retryer takes those options, returns a promise and will resolve or reject it once the whole process of fetching is complete. This takes retries, network availability etc. into account. Fore example, the query might go into paused state in between and continue with retries later.

So the retryer will see the options from the time it was created (=when the fetch started). Usually, this is what you want because the retry process is opaque. The promise will resolve or reject eventually, and then, when a refetch occurs (which is different from a retry), the new values will be respected.

We could make those values reactive in between retries, but I think that'll open up a can of worms. What if you change retries from 5 to 2 while you are at the 3rd retry? What if you change networkMode to 'always' while the query is paused because you have no network connection? It's the safer approach to just trigger the whole process (fetch + retries) with a given set of options from creation time until it is finished. It's a bit how closures work with react functions, too.

Both retry and retryDelay can be a function, so you can define them as a function and use a ref to read the latest value inside of it if you really want.

from query.

peVelosa avatar peVelosa commented on May 28, 2024

I had the same issue trying to update a variable inside useQuerywhitout updating the queryKey

useQuery will only update the variables within it if the queryKey is changed. As it stands, even changing the retryDelay the query will not have its values โ€‹โ€‹updated, since the queryKey has not changed
If you change line 12 to this one you will see that the problem has been solved

    queryKey: ['test', retryDelay],

refetchInterval is used to revalidate the query, so it will only work if it is successful.

I think that it happens because of the caching in react-query. The function will not update is own values if the key doesn't change, it will only update the data.

Maybe I'm making a mistake trying to prove you my point (I've heard it while a go, so that is the reason that I know it), but maybe the useQuery component will only trigger the useEffect and rerender it state when some of the options in queryKey, queryFn changes.

image

https://github.com/TanStack/query/blob/main/packages/react-query/src/useBaseQuery.ts#L91

from query.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.