Feature request: De-/hydration with TypeScript (dehydrate does not take a generic for state type, hydration is not type-safe) #2872
Unanswered
bennettdams
asked this question in
Q&A
Replies: 1 comment 4 replies
-
It is also hard to type it on a custom App, there is not such How can i properly type the hydration properties? Where
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would love to do the hydration in a type-safe manner.
For the Next.js example,
<Hydration>..
is used in_app.jsx
. This means that React Query's hydration can only ever work, if you never have unserializable data in any of your pages ingetStaticProps
getServerSideProps
, e.g. the very firstDate
will make the hydration fail.If we now say that one has to de-/serialize on his own (like written in the limitations),
_app
is a bad place to do that, because the data will look different for every page, so handling the deserialization in this general entrypoint component is probably not a good idea.What I did now was using
<Hydrate>..
in the page where I also fill the query cache. That way I also have the deserialization in the same place, which allows type-safish execution.Do you have a recommendation to make the deserialization type-safe? Right now, neither
dehydrate
or<Hydrate>..
are implemented to be provided with a generic, so theQueryState
's data is alwaysunknown
.Example:
Proposed solution
I think, for the beginning, it would be enough if
dehydrate
would take a generic that is passed through to the internalQueryState
(which already takes a generic). That way, you can at least do the deserialization in a type-safe manner. This would not make alle the hydration type-safe, but as the serialization will be done with a general function (likeJSON.parse(JSON.stringify(..))
) most of the time, thedehydrate
function is more important. Look for the// deserialization
comment in my code example above, that is the only place where knowing the type is really important. Without knowing the type there, you would've to create a "generic" function that transforms all e.g. ISO strings to dates based on keys or something - when you know the type, you can do custom deserialization, which is the better approach in my opinion.Beta Was this translation helpful? Give feedback.
All reactions