Git Product home page Git Product logo

Comments (3)

TkDodo avatar TkDodo commented on May 31, 2024 1

oh interesting. So conceptually, I think this is how it's designed to work. initialData is only taken into account when the Query is created in the cache, which in your case happens when you call prefetchQuery.

When your component renders, it will pick up the already existing Query and initialData is ignored.

So overall, this is a typings issue at best. I was very reluctant to add any guarantees on data always being defined, and really thought that when initialData is provided, nothing can go wrong. But I guess there are cases like the one you've described.

However, I don't think we're going back on the type definitions to make them T | undefined even if initialData is provided. It's too good of a dx improvement 99% of the time.

What you probably want in your case is placeholderData, which is always taken in to account and doesn't narrow the type.

https://codesandbox.io/p/sandbox/react-query-with-initial-data-forked-whffv2

from query.

TkDodo avatar TkDodo commented on May 31, 2024 1

well with queryOptions, we can infer the return type when a key is used depending on what the quryFn returns. But that doesn't help us with adding | undefined to the union or not

from query.

FrikkieSnyman avatar FrikkieSnyman commented on May 31, 2024

Interesting discussion.

Since this is a typings issue, I do wonder if there's some consideration to infer return type based on the query key? Perhaps this already digresses to a discussion on key management, but I do believe that in almost all cases, the expectation would be that a single return type exists given a key, regardless of that key being used in prefetchQuery or useQuery.

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.