Alpha 2
This release brings query result selectors (with structural sharing), code generation, a module system and lots of other improvements and updates to the still-WIP APIs.
Changes
selectFromResult
option for useQuery
We've had some discussion & requests around the possibility to just subscribe to part of a query result instead of the full result and isFetching
/isSuccess
/etc., to have more granular rerenders.
This is now possible with the selectFromResult
option to useQuery
.
One thing that we had to add to make this possible is "structural sharing".
So, if you have a second request that comes back with (partially) similar data, we try to recycle as many object references from the last query result as possible, to prevent unneccessary rerender.
If you want to only select query result data or only trigger a query without caring for any result, you can now use the useQueryState
and useQuerySubscription
hooks. (#106)
Code generation
Another big thing is code generation. You can now generate full API definitions from OpenAPI definitions. Big thanks to @fgnass who let us use the type generation from his oazapfts
library.
If you want to give it a try, run curl -o petstore.json https://petstore3.swagger.io/api/v3/openapi.json; npx @rtk-incubator/rtk-query-codegen-openapi petstore.json > petstore-api.generated.ts
.
Docs are over at https://rtk-query-docs.netlify.app/concepts/code-generation
Modularization & new build
The third big thing is a new build configuration to enable tree-shaking. Bundle sizes are significantly down due to that. Also, RTK-Query has been modularized, so you can now use createBaseApi
if you don't need the React hooks
The module system also allows you to write your own createApi
plugins. See https://rtk-query-docs.netlify.app/concepts/customizing-create-api
More granular hooks refetch behaviour
In addition, we've added some new options to tweak the refetch behaviour of the hooks.
There now are refetchOnFocus
and refetchOnReconnect
options for the hooks. (#90)
Also, we now have a refetchOnMountOrArgChange
option to trigger a refetch when a component is mounted/changes its query argument even if there is already data in the cache for the target route. You can specify a "minumum age" here. (#80)
Other changes
-
We had a bug report & fix PR from https://twitter.com/PettengillAaron for the edge case when an api returned an empty response body. Parsing that as JSON was probably not a good idea. Thanks! (#91)
-
You can now pass in a custom
fetch
function intofetchBaseQuery
- this came up in a discussion on server side rendering as a way to make better use offetch
ponyfills. Thanks to https://twitter.com/JoshMarantz for bringing this up. (#105) -
You can now call
.unwrap
on the result from theuseMutation
hook andmutation.initiate
action creator to have the promise resolve with the returned value or throw an exception. If people like this API, we might add this to RTKscreateAsncThunk
as an alternative tounwrapResult
. Thanks for https://twitter.com/kahirokunn for bringing this up. (#117)
Also, be aware: this changed the general return value of the result from theuseMutation
hook from the resulting state to{data: ...} | {error: ...}
in case you're not unwrapping. -
prepareHeaders
can now be an async function. Thanks to https://github.com/schadenn for the contribution. (#123) -
Query endpoint definitions now support the same
onStart
,onError
andonSuccess
options that mutation endpoints already did. Thanks to https://twitter.com/kahirokunn. (#130) -
api.internalActions.prefetchThunk
was renamed toapi.util.prefetchThunk
(#133)
A big thanks to everyone I forgot to mention here who discussed with us on github, in @reactiflux, or here on Twitter.
And an enormous "thank you" to https://twitter.com/de_stroy who did an incredible amount of PRs, Code Reviews and Documentation.