Releases: reduxjs/redux-toolkit
v1.7.0-rc.0
This release candidate fixes several assorted small issues and updates dependencies.
Assuming no other problems pop up, we plan on releasing 1.7 in the next couple days.
npm i @reduxjs/toolkit@next
yarn add @reduxjs/toolkit@nextChangelog
RTK Query Fixes
Fixed an issue serializing a query arg of undefined. Related, an empty JSON body now is stored as null instead of undefined.
There are now dev warnings for potential mistakes in endpoint setup, like a query function that does not return a data field.
Lazy query trigger promises can now be unwrapped similar to mutations.
api.util.resetApiState will now clear out cached values in useQuery hooks.
The RetryOptions interface is now exported, which resolves a TS build error when using the hooks with TS declarations.
Dependency Updates
Updated to Immer ^9.0.7, Reselect ^4.1.5, and Thunk ^2.4.1 to pick up the latest types and bug fixes.
Also, the peer dependencies now list React 18 beta and React-Redux 8 beta as acceptable versions.
Other Fixes
The isPlainObject util has been updated to match the implementation in other Redux libs.
The UMD builds of RTK Query now attach as window.RTKQ instead of overwriting window.RTK.
Fixed an issue with sourcemap loading due to an incorrect filename replacement.
What's Changed
- fetchBaseQuery: return nullon empty body for JSON. Add DevWarnings by @phryneas in #1699
- Add unwrap to QueryActionCreatorResult and update LazyQueryTrigger by @msutkowski in #1701
- Only set originalArgs if they're not undefined by @phryneas in #1711
- Treat null as a valid plain object prototype in isPlainObject() in order to sync the util across reduxjs/* repositories by @Ilyklem in #1734
- export RetryOptions interface from retry.ts by @colemars in #1751
- fix: api.util.resetApiState should reset useQuery hooks by @phryneas in #1735
- fix issue where the global RTK object got overwritten in the UMD files by @Antignote in #1763
- Update dependencies and selector types by @markerikson in #1772
- Fix broken sourcemap output due to bad filename replacement by @markerikson in #1773
Full Changelog: v1.7.0-beta.1...v1.7.0-rc.0
v1.7.0-beta.1
This beta release updates createSlice to avoid potential circular dependency issues by lazy-building its reducer, and updates our runtime dependencies to their latest versions.
npm i @reduxjs/toolkit@next
yarn add @reduxjs/toolkit@nextChangelog
createSlice Lazy Reducers and Circular Dependencies
For the last couple years we've specifically recommended using a "feature folder" structure with a single "slice" file of logic per feature, and createSlice makes that pattern really easy - no need to have separate folders and files for /actions and /constants any more.
The one downside to the "slice file" pattern is in cases when slice A needs to import actions from slice B to respond to them, and slice B also needs to listen to slice A. This circular import then causes runtime errors, because one of the modules will not have finished initializing by the time the other executes the module body. That causes the exports to be undefined, and createSlice throws an error because you can't pass undefined to builder.addCase() in extraReducers. (Or, worse, there's no obvious error and things break later.)
There are well-known patterns for breaking circular dependencies, typically requiring extracting shared logic into a separate file. For RTK, this usually means calling createAction separately, and importing those action creators into both slices.
While this is a rarer problem, it's one that can happen in real usage, and it's also been a semi-frequently listed concern from users who didn't want to use RTK.
We've updated createSlice to now lazily create its reducer function the first time you try to call it. That delay in instantiation should eliminate circular dependencies as a runtime error in createSlice.
We'd appreciate users trying this out and seeing if it successfully fixes that problem. If you previously extracted some separate actions due to circular dep issues, please try re-consolidating those into the actual slices and see how it works.
Dependency Updates
We've updated our deps to the latest versions:
- Reselect 4.1.x: Reselect has brand-new customization capabilities for selectors, including configuring cache sizes > 1 and the ability to run equality checks on selector results. It also now has completely rewritten TS types that do a much better job of inferring arguments and catch previously broken patterns.
- Redux Thunk 2.4.0: The thunk middleware also has improved types, as well as an optional "global override" import to modify the type of
Dispatcheverywhere in the app
We've also lowered RTK's peer dependency on React from ^16.14 to ^16.9, as we just need hooks to be available.
What's Changed
- Update Yarn from 2.4 to 3.1 by @markerikson in #1688
- allow for circular references by building reducer lazily on first reducer call by @phryneas in #1686
- Update deps for 1.7 by @markerikson in #1692
Full Changelog: v1.7.0-beta.0...v1.7.0-beta.1
v1.7.0-beta.0
This release updates RTK Query with support for SSR and rehydration, allows sharing mutation results across components, adds a new currentData field to query results, adds several new options for customizing endpoints and base queries, adds support for async condition options in createAsyncThunk, and updates createSlice/createReducer to accept a "lazy state initializer" function.
npm i @reduxjs/toolkit@next
yarn add @reduxjs/toolkit@nextSee the v1.7 beta docs for updated usage guides and API references:
Changelog
RTK Query SSR and Rehydration Support
RTK Query now has support for SSR scenarios, such as the getStaticProps/getServerSideProps APIs in Next.js. Queries can be executed on the server using the existing dispatch(someEndpoint.initiate()) thunks, and then collected using the new await Promise.all(api.getRunningOperationPromises()) method.
API definitions can then provide an extractRehydrationInfo method that looks for a specific action type containing the fetched data, and return the data to initialize the API cache section of the store state.
The related api.util.getRunningOperationPromise() API adds a building block that may enable future support for React Suspense as well, and we'd encourage users to experiment with this idea.
Sharing Mutation Results Across Components
Mutation hooks provide status of in-progress requests, but as originally designed that information was unique per-component - there was no way for another component to see that request status data. But, we had several requests to enable this use case.
useMutation hooks now support a fixedCacheKey option that will store the result status in a common location, so multiple components can read the request status if needed.
This does mean that the data cannot easily be cleaned up automatically, so the mutation status object now includes a reset() function that can be used to clear that data.
Data Loading Updates
Query results now include a currentData field, which contains the latest data cached from the server for the current query arg. Additionally, transformResponse now receives the query arg as a parameter. These can be used to add additional derivation logic in cases when a hooks query arg has changed to represent a different value and the existing data no longer conceptually makes sense to keep displaying.
Data Serialization and Base Query Improvements
RTK Query originally only did shallow checks for query arg fields to determine if values had changed. This caused issues with infinite loops depending on user input.
The query hooks now use a "serialized stable value" hook internally to do more consistent comparisons of query args and eliminate those problems.
Also, fetchBaseQuery now supports a paramsSerializer option that allows customization of query string generation from the provided arguments, which enables better interaction with some backend APIs.
The BaseQueryApi and prepareheaders args now include fields for endpoint name, type to indicate if it's a query or mutation, and forced to indicate a re-fetch even if there was already a cache entry. These can be used to help determine headers like Cache-Control: no-cache.
createAsyncThunk Improvements
The condition option may now be async, which enables scenarios like checking if an existing operation is running and resolving the promise when the other instance is done.
If an idGenerator function is provided, it will now be given the thunkArg value as a parameter, which enables generating custom IDs based on the request data.
The createAsyncThunk types were updated to correctly handle type inference when using rejectWithValue().
Other Improvements
createSlice and createReducer now accept a "lazy state initializer" function as the initialState argument. If provided, the initializer will be called to produce a new initial state value any time the reducer is given undefined as its state argument. This can be useful for cases like reading from localStorage, as well as testing.
API objects now have a selectInvalidatedBy function that accepts a root state object and an array of query tag objects, and returns a list of details on endpoints that would be invalidated. This can be used to help implement optimistic updates of paginated lists.
Related Libraries
The Redux team has also recently released Reselect 4.1 and Redux Thunk 2.4. Reselect 4.1 contains major improvements to selector options, including cache sizes > 1, and both libraries have improved TS types. We'll update 1.7 to depend on those new versions before release, but you can update your own projects to make sure you have the new functionality and types available as well:
- https://github.com/reduxjs/reselect/releases/tag/v4.1.0
- https://github.com/reduxjs/redux-thunk/releases/tag/v2.4.0
What's Changed
- fix "isLoading briefly flips back to
true" #1519 by @phryneas in #1520 - feat(createAsyncThunk): async condition by @thorn0 in #1496
- add
argtotransformResponseby @phryneas in #1521 - add
currentDataproperty to hook results. by @phryneas in #1500 - use
useSerializedStableValuefor value comparison by @phryneas in #1533 - fix(useLazyQuery): added docs for preferCache option by @akashshyamdev in #1541
- correctly handle console logs in tests by @phryneas in #1567
- add
resetmethod to useMutation hook by @phryneas in #1476 - allow for "shared component results" using the
useMutationhook by @phryneas in #1477 - 🐛 Fix bug with
useMutationshared results by @Shrugsy in #1616 - pass the ThunkArg to the idGenerator function by @loursbourg in #1600
- Support a custom paramsSerializer on fetchBaseQuery by @msutkowski in #1594
- SSR & rehydration support, suspense foundations by @phryneas in #1277
- add
endpoint,typeandforcedtoBaseQueryApiandprepareHeadersby @phryneas in #1656 - split off signature without
AsyncThunkConfigfor better inference by @phryneas in #1644 - Update createReducer to accept a lazy state init function by @markerikson in #1662
- add
selectInvalidatedByby @phryneas in #1665
Full Changelog: v1.6.2...v1.7.0-beta.0
v1.6.2
This release fixes several small issues with RTK Query, as well as a regression in the createAsyncThunk types and an issue with sourcemap URLs.
Changelog
RTK Query Fixes
The isLoading flag should only ever be true on the first run of a hook, but would sometimes briefly flip to true on later calls. That should now stay the correct value.
fetchBaseQuery should now work properly when used in conjunction with node-fetch.
The BaseQueryApi object now correctly includes the extra argument that was provided when configuring the thunk middleware, if any.
Other Fixes
Sourcemap URLs should now be correct, especially for the CommonJS build artifacts.
createAsyncThunk's types have been updated to correctly infer return values when working with enums.
Lots of assorted docs tweaks and updates!
What's Changed
- Add extra to BaseQueryApi by @ricksanchez in #1378
- fix: point sourceMappingURL to correct sourcemaps in build artifacts by @jawadsh123 in #1459
- fix:
createAsyncThunkunion return values fall back to allowing only single member by @phryneas in #1449 - fix
fetchBaseQueryfor usage withnode-fetchby @phryneas in #1473 - fix "isLoading briefly flips back to
true" #1519 by @phryneas in #1520
Full Changelog: v1.6.1...v1.6.2
v1.6.1
This release improves several edge cases in RTK Query behavior and implementation, deprecates a lesser-used API, and reverts an internal compatability change from 1.6.
Changelog
RTK Query Tweaks
We've made several small tweaks to the RTK Query implementation:
fetchBaseQuerynow provides a more meaningful error if the response can't be parsed successfullyfetchBaseQueryhas been tweaked to always readfetchfrom the global scope, rather than closing over it at creation time. This improves usage with test tools that mock or overridefetchat the system level, such as Mirage.- The
skipTokensymbol is now created usingSymbol.for(), to get a consistent reference - API slices now warn if you try to add more than one reducer with the same
reducerPathname - An internal hook usage was tweaked to avoid the "don't call
useLayoutEffecton the server" warning being printed in SSR
Also, mutations no longer track the originalArgs value in the store. That value is needed to re-run queries, but since mutations are not re-run, it wasn't needed. This change resolves cases where users were passing a non-serializable value as the mutation argument and then seeing warnings about it being put into the store.
Technically, this is a breaking change (removes a store property what would have been returned by a selector), but it is a necessary bugfix, and it does not appear anyone was actively using that property. So, we're keeping this as a patch release.
Generally, the information removed is still available as:
- a property on the promise returned by
dispatch - part of the thunk action
meta - return value of the
useMutationhook
Other Changes
The typings for createAction and createAsyncThunk have been tweaked to avoid lint warnings about "unbound methods".
The exported version of getDefaultMiddleware is now marked as deprecated, and will be removed in a future 2.0 release. Use the function passed as the middleware callback instead, which has the correct store types anyway.
In 1.6, we moved the Immer enableES5 plugin init call from index.ts to be inside of createReducer instead, in an effort to maybe save a few bytes for some users. This has caused some issues for users who still support IE11, possibly due to build config issues. Realistically, we expect that everyone who uses RTK will be calling createReducer, createSlice, or createApi at some point, so there's no real situations where this wouldn't be called anyway. So, we've moved the enableES5 call back to index.ts for consistency. In a future 2.0 release, we will remove that call entirely, and users that still support IE11 will need to call that themselves.
Changes
- Error handling of fetchBaseQuery (#1250 - @phryneas)
- Warn on duplicate
reducerPath(#1252 - @phryneas) - Deprecate
getDefaultMiddlewareexport (#1258 - @Shrugsy) - Typing for unbound functions (#1263 - @ajcrites)
- Prevent closing over
fetch(#1267 - @Shrugsy) - Put
enableES5back inindex.ts(#1305 - @komar94) - Use
Symbol.for('skipToken')(#1317 - @phryneas) - Remove
originalArgs(#1318 - @phryneas) - Call useIsomorphicLayoutEffect to fix warnings (#1319 - @markerikson)
v1.6.0 : RTK Query!
This release adds the new RTK Query data fetching APIs to Redux Toolkit. It also adds multiple new options to createAsyncThunk for including meta fields and working with results, updates dependencies to Redux 4.1 and Immer 9, and includes a complete rewrite of our build toolchain with additional "modern" build artifacts in the package.
While this is a minor release in terms of semver, this is a huge update in terms of functionality, scope, and effort. We're excited about how these new APIs will help our users build better applications with less code and better behavior!
Installation:
npm i @reduxjs/toolkit@latest
yarn add @reduxjs/toolkit@latestUpgrade Note: During the alphas, we received some reports of users seeing incorrect types after installing the RTK 1.6 previews. The problems appeared to be caused by multiple versions of the
reduxpackage ending up in a project'snode_modulesfolder. If you see this issue, you may need to uninstall and reinstallreact-reduxwith the latest version, to help ensure noreduxduplicates are in the package tree.
Changelog
RTK Query Data Caching API
RTK Query is a powerful data fetching and caching tool. It is designed to simplify common cases for loading data in a web application, eliminating the need to hand-write data fetching & caching logic yourself.
RTK Query is an optional addon included in the Redux Toolkit package, and its functionality is built on top of the other APIs in Redux Toolkit.
See the RTK Query usage guides and API reference docs for complete information on how to use RTK Query:
https://redux-toolkit.js.org/rtk-query/overview
Motivation
Web applications normally need to fetch data from a server in order to display it. They also usually need to make updates to that data, send those updates to the server, and keep the cached data on the client in sync with the data on the server. This is made more complicated by the need to implement other behaviors used in today's applications:
- Tracking loading state in order to show UI spinners
- Avoiding duplicate requests for the same data
- Optimistic updates to make the UI feel faster
- Managing cache lifetimes as the user interacts with the UI
The Redux core has always been very minimal - it's up to developers to write all the actual logic. That means that Redux has never included anything built in to help solve these use cases. The Redux docs have taught some common patterns for dispatching actions around the request lifecycle to track loading state and request results, and Redux Toolkit's createAsyncThunk API was designed to abstract that typical pattern. However, users still have to write significant amounts of reducer logic to manage the loading state and the cached data.
Over the last couple years, the React community has come to realize that "data fetching and caching" is really a different set of concerns than "state management". While you can use a state management library like Redux to cache data, the use cases are different enough that it's worth using tools that are purpose-built for the data fetching use case.
RTK Query takes inspiration from other tools that have pioneered solutions for data fetching, like Apollo Client, React Query, Urql, and SWR, but adds a unique approach to its API design:
- The data fetching and caching logic is built on top of Redux Toolkit's
createSliceandcreateAsyncThunkAPIs - Because Redux Toolkit is UI-agnostic, RTK Query's functionality can be used with any UI layer
- API endpoints are defined ahead of time, including how to generate query parameters from arguments and transform responses for caching
- RTK Query can also generate React hooks that encapsulate the entire data fetching process, provide
dataandisLoadingfields to components, and manage the lifetime of cached data as components mount and unmount - RTK Query provides "cache entry lifecycle" options that enable use cases like streaming cache updates via websocket messages after fetching the initial data
- We have early working examples of code generation of API slices from OpenAPI and GraphQL schemas
- Finally, RTK Query is completely written in TypeScript, and is designed to provide an excellent TS usage experience
Basic Usage
RTK Query is included within the installation of the core Redux Toolkit package. It is available via either of the two entry points below:
import { createApi } from '@reduxjs/toolkit/query'
/* React-specific entry point that automatically generates
hooks corresponding to the defined endpoints */
import { createApi } from '@reduxjs/toolkit/query/react'For typical usage with React, start by importing createApi and defining an "API slice" that lists the server's base URL and which endpoints we want to interact with:
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { Pokemon } from './types'
// Define a service using a base URL and expected endpoints
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemonByName: builder.query<Pokemon, string>({
query: (name) => `pokemon/${name}`,
}),
}),
})
// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const { useGetPokemonByNameQuery } = pokemonApiThe "API slice" also contains an auto-generated Redux slice reducer and a custom middleware that manages suscription lifetimes. Both of those need to be added to the Redux store:
import { configureStore } from '@reduxjs/toolkit'
// Or from '@reduxjs/toolkit/query/react'
import { setupListeners } from '@reduxjs/toolkit/query'
import { pokemonApi } from './services/pokemon'
export const store = configureStore({
reducer: {
// Add the generated reducer as a specific top-level slice
[pokemonApi.reducerPath]: pokemonApi.reducer,
},
// Adding the api middleware enables caching, invalidation, polling,
// and other useful features of `rtk-query`.
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(pokemonApi.middleware),
})
// optional, but required for refetchOnFocus/refetchOnReconnect behaviors
// see `setupListeners` docs - takes an optional callback as the 2nd arg for customization
setupListeners(store.dispatch)Finally, import the auto-generated React hooks from the API slice into your component file, and call the hooks in your component with any needed parameters. RTK Query will automatically fetch data on mount, re-fetch when parameters change, provide {data, isFetching} values in the result, and re-render the component as those values change:
import * as React from 'react'
import { useGetPokemonByNameQuery } from './services/pokemon'
export default function App() {
// Using a query hook automatically fetches data and returns query values
const { data, error, isLoading } = useGetPokemonByNameQuery('bulbasaur')
// render UI based on data and loading state
}Bundle Size
RTK Query adds a fixed one-time amount to your app's bundle size. Since RTK Query builds on top of Redux Toolkit and React-Redux, the added size varies depending on whether you are already using those in your app. The estimated min+gzip bundle sizes are:
- If you are using RTK already: ~9kb for RTK Query and ~2kb for the hooks.
- If you are not using RTK already:
- Without React: 17 kB for RTK+dependencies+RTK Query
- With React: 19kB + React-Redux, which is a peer dependency
Adding additional endpoint definitions should only increase size based on the actual code inside the endpoints definitions, which will typically be just a few bytes.
The functionality included in RTK Query quickly pays for the added bundle size, and the elimination of hand-written data fetching logic should be a net improvement in size for most meaningful applications.
Build Tooling Improvements
We've completely replaced our previous TSDX-based build tooling pipeline with a custom build pipeline based on ESBuild and TypeScript. This should have no visible changes behavior-wise for end users - we've kept the same build artifact names and ES syntax levels. However, it does speed up our own build process, which is important now that we're generating many more output files.
The published package now also includes a set of "modern" ESM build artifacts that target ES2017 syntax instead of ES5. These files should be smaller than the current "ESM" artifact, which is uses the ES module format but with ES5-level syntax for backwards compatibility.
Most bundlers should currently pick up the ESM artifact as the default, such as in Create-React-App projects. If you are planning to drop IE11 compatibility, you should be able to modify your bundler config to import the modern artifact instead. Since the modern artifact includes the usual process.env.NODE_ENV checks for build tools to use, we also have pre-compiled versions for "modern dev" and "modern prod" that are suitable for use in browsers or ESM-centric build tools.
We've also done an optimization pass on both the RTK core and the RTK Query sections to improve tree shaking.
See this table for details on the generated artifacts, which are available for each of the entry points:
Redux Toolkit Build Artifacts
| Filename | Module | Syntax | process.env | Purpose |
|----------------------------------------|--------|-------...
v1.6.0-rc.1
This release candidate finalizes the upcoming RTK Query APIs. It adds new options to createAsyncThunk for adding meta fields to thunk-dispatched actions, updates the createApi lifecycle handlers to pass through those meta fields, and removes fields that were deprecated during the alpha process. We've also fleshed out the RTKQ preview docs with significant new content.
This should be the final preview before the RTK 1.6 final release, barring any unforeseen last-minute bugs. (Note: This is the same content as rc.0, but we found a bug in our build process that had wrong output in that release. Hopefully no more issues!)
The preview docs are located at https://deploy-preview-1016--redux-starter-kit-docs.netlify.app .
Installation:
npm i @reduxjs/toolkit@next
yarn add @reduxjs/toolkit@nextChangelog
Async Thunk meta Support
createAsyncThunk automatically generates action creators and action types, and then automatically dispatches those actions during execution. This simplifies the process of creating and using thunks, but like all abstractions, also limits flexibility.
One limitation has been that there was no way to customize the meta field to the actions generated by createAsyncThunk, and some users needed to add additional metadata to actions for use by other middleware.
We've updated createAsyncThunk to allow adding additional contents to meta. The approach varies based on the action type:
pending: there is a newgetPendingMeta({arg, requestId})callback that can be passed as part of thecreateAsyncThunkoptions object. This is necessary because thependingaction is dispatched before the payload creator is even called.fulfilled: there is a newfulfillWithMetautility in the payload creator'sthunkApiobject, which can be used instead of returning the payload directly:return fulfillWithMeta(actualPayload, meta)rejected: the existingrejectWithValueutility now also accepts ametaargument:return rejectWithValue(failedPayload, meta)
API Lifecycle meta Support
The createApi cache lifecycle callbacks like onCacheEntryAdded now make the meta field available as part of the promise result, such as cacheDataLoaded.
Code Cleanup and Types Tweaks
The fields such as onStart and onSuccess that were deprecated in earlier alphas have been removed.
The now-unused ApiWithInjectedEndpoints type was removed.
Various APIs that accept arrays were updated to mark those array parameters as readonly, to help indicate that they shouldn't be mutated, and to ensure that arrays marked as const can be accepted.
The ApiModules type was modified to prevent a inferred type of this node exceeds the maximum length the compiler will serialize TS error.
Docs Updates
We've updated to Docusaurus 2.0-beta.0, which includes Webpack 5. No visible change for you, but faster CI builds for us thanks to build artifact caching! (Our docs builds dropped from around 7 minutes each build down to around 25 seconds, thanks to only changed files being rebuilt.)
We've also completed filling out the RTK Query API docs.
Changes
Code
- Remove upcoming alpha.3 deprecations (#1052 - @phryneas)
- createAsyncThunk: support for
meta(#1083 - @phryneas) - Add baseQueryMeta to thunks, meta to lifecycle results (#1083 - @phryneas)
- Remove ApiWithInjectedEndpoints references (#1112 - @msutkowski)
- Add
readonlyto array-type function params (#1113 - @phryneas) - remove
Idto force lazy evaluation of API type (#1116 - @phryneas)
Docs
- Wayyyy too many docs PRs to even try to list here :) (@Shrugsy)
- Wayyyy too many examples moved from sandboxes to even try to list here :) (@msutkowski)
- Match redux config for docusaurus webpack v5 (#1086 - @msutkowski)
- Some assorted content tweaks (@markerikson)
v1.6.0-rc.0
This release is broken due to a build misconfiguration - please see rc.1 instead:
https://github.com/reduxjs/redux-toolkit/releases/tag/v1.6.0-rc.1
v1.6.0-beta.0
This beta release improves the in-progress RTK Query APIs. It adds a new onCacheEntryAdded lifecycle callback to enable streaming cache updates, adds per-endpoint cache timeout overrides and additional options for skipping queries, and fixes issues with query arg serialization and build output, We've also fleshed out the RTKQ preview docs with significant new content.
We are hopeful that this will be the final pre-release with any meaningful API changes, and that the remaining work should just be final polish of the documentation before this goes live.
The preview docs are located at https://deploy-preview-1016--redux-starter-kit-docs.netlify.app .
Installation:
npm i @reduxjs/toolkit@next
yarn add @reduxjs/toolkit@nextChangelog
Cache Entry Lifecycle
RTK Query is built around standard HTTP endpoint request/response-style API calls. However, today's applications often use a hybrid approach, where initial data is fetched via a request, but then further updates are streamed via a websocket or other persistent connection.
Endpoint definitions now support an onCacheEntryAdded lifeycle callback. This callback will be executed whenever a new endpoint subscription entry is added to the cache (ie, when a component requests a specific endpoint+params combination that is not currently being loaded).
The onCacheEntryAdded callback allows you to run additional logic after the initial fetch completes and after the entry is removed from the cache, and provides tools to update the existing cache for this query as needed. The intended use case is to open a websocket-type connection, and update the cached data over time as messages are received. A typical usage might look like:
export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/" }),
endpoints: (build) => ({
getMessages: build.query({
query: (channel) => `messages/${channel}`,
async onCacheEntryAdded(
arg,
{ updateCachedData, cacheDataLoaded, cacheEntryRemoved }
) {
// wait for the initial query to resolve before proceeding
await cacheDataLoaded;
// Update our query result when messages are received
const unsubscribe = ChatAPI.subscribeToChannel(
arg.channelId,
(message) => {
// Dispatches an update action with the diff
updateCachedData((draft) => {
draft.push(message);
});
}
);
// Clean up when cache subscription is removed
await cacheEntryRemoved;
unsubscribe();
},
}),
}),
});This adds significant additional flexibility in interacting with cached data.
Additional API Polish
createApi supports a keepUnusedDataFor option to modify the default cache expiration time, but that can now be overridden on a per-endpoint basis as well.
The selectFromResult option has been reworked so that it receives the standard hook result structure as its input, and you can extract and return whatever pieces of that data are actually needed by this component.
RTKQ now exports a skipToken value that can be passed to queries as an indicator that the query should be skipped for now, in addition to the existing skip flag option. This is primarily useful for working around some TS inference issues with hook return types.
The copyWithStructuralSharing util is now exported.
The updateQueryResult and pathQueryResult util methods have been renamed to updateQueryData and patchQueryData.
Optimistic updates can now be reverted by calling .undo(), which automatically dispatches the appropriate inverse patch update action.
Fixes
The MiddlewareArray type has been tweaked to produce correct behavior when transpiled.
The default query arg serialization logic now handles nested values correctly.
Docs Updates
We've significantly improved the preview RTK Query documentation. We've added pages on "Streaming Updates", "Cached Data", "Customizing Queries", and "Migrating to RTK Query". We've also fleshed out the API references for the generated React hooks, added more details to descriptions of queries and endpoints, and filled out info on how cache lifetime behavior works. Thanks to @Shrugsy for writing most of the docs updates!
Changes
Code
- export Result Definition types (#1022 - @phryneas)
- add export for
copyWithStructuralSharing(#1026 - @phryneas) - fix/default serialize query args (#1029 - @phryneas)
- split up middleware, add OptionalPromise, add cache entry lifecycle (#1034 - - @phryneas)
- simplify
selectFromResultusage (#1035 - @phryneas) - fix MiddlewareArray for new build (#1038 - @phryneas)
- remove last traces of TSDX (#1039 - @phryneas)
- prevent badly typed
Middleware<any>from castingdispatchtoany(#1042 - @phryneas) - remove alpha.2 deprecation fallbacks (#1051 - @phryneas)
- add a per-endpoint keepUnusedDataFor option (#1053 - @phryneas)
BaseQueryApi.signalis not optional (#1058 - @phryneas)- allow using skipSymbol as arg in hooks (#1056 - @phryneas)
- nest data structure for
cacheDataLoadedandqueryFulfilled(#1078 - @phryneas)
Docs
- Docs - add 'Cached data' concept page (#1036 - @Shrugsy)
- Docs - RTKQ - Convert codeblocks to TS & enable codeblock transpilation (#1042 - @Shrugsy)
- Docs - add Streaming Updates page (#1049 - @Shrugsy)
- Docs - add "Customizing Queries" page (#1057 - @Shrugsy)
- Docs - Add "Migrating to RTK Query" page (#1060 - @Shrugsy)
- Docs - add RTK Query content to "Getting Started" page (#1066 - @Shrugsy)
- Docs - expand explanation of cache lifetime and subscription behavior (#1071 - @Shrugsy)
- Docs - extend detail for Queries and Endpoints (#1074 - @Shrugsy)
v1.6.0-alpha.2
This release fixes a bug with RTK Query cache invalidation, and exposes the useLazyQuerySubscription hook.
Installation:
npm i @reduxjs/toolkit@next
yarn add @reduxjs/toolkit@nextChangelog
Invalidation Bugfix
We saw that RTK Query cache invalidation was actually broken in alpha.1. After investigation, it looks like TypeScript was outputting incorrect code for looping over a set.values() iterator. We've tweaked the logic to work around that.
Please upgrade to this release ASAP, as invalidation is a key part of RTK Query's functionality.
useLazyQuerySubscription
The useLazyQuerySubscription hook was documented, but wasn't actually being included in the output. We've fixed that.
Additional Build Tweaks
We're still fiddling with the combination of ESBuild and TypeScript for our recently updated build chain, and have flipped a couple more switches in the process. Should be no user-visible difference.
Changes
- Expose
useLazyQuerySubscriptionfor endpoints ( #1017 - @Shrugsy) - Fix bad transpilation of set iteration breaking invalidation (#1020 - @markerikson)