You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/configureStore.mdx
+22-2Lines changed: 22 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,28 @@ hide_title: true
9
9
10
10
# `configureStore`
11
11
12
-
A friendly abstraction over the standard Redux `createStore` function that adds good defaults
13
-
to the store setup for a better development experience.
12
+
The standard method for creating a Redux store. It uses the low-level Redux core `createStore` method internally, but wraps that to provide good defaults to the store setup for a better development experience.
13
+
14
+
## Purpose and Behavior
15
+
16
+
A standard Redux store setup typically requires multiple pieces of configuration:
17
+
18
+
- Combining the slice reducers into the root reducer
19
+
- Creating the middleware enhancer, usually with the thunk middleware or other side effects middleware, as well as middleware that might be used for development checks
20
+
- Adding the Redux DevTools enhancer, and composing the enhancers together
21
+
- Calling `createStore`
22
+
23
+
Legacy Redux usage patterns typically required several dozen lines of copy-pasted boilerplate to achieve this.
24
+
25
+
Redux Toolkit's `configureStore` simplifies that setup process, by doing all that work for you. One call to `configureStore` will:
26
+
27
+
- Call `combineReducers` to combine your slices reducers into the root reducer function
28
+
- Add the thunk middleware and called `applyMiddleware`
29
+
- In development, automatically add more middleware to check for common mistakes like accidentally mutating the state
30
+
- Automatically set up the Redux DevTools Extension connection
31
+
- Call `createStore` to create a Redux store using that root reducer and those configuration options
32
+
33
+
`configureStore` also offers an improved API and usage patterns compared to the original `createStore` by accepting named fields for `reducer`, `preloadedState`, `middleware`, `enhancers`, and `devtools`, as well as much better TS type inference.
Conceptually, eachslicereducer"owns"itssliceofstate. There's also a natural correspondance between the update logic defined inside `reducers`, and the action types that are generated based on those.
However, unlikethe`reducers`field, eachindividualcasereducerinsideof`extraReducers`will_not_generateanewactiontypeoractioncreator.
333
+
334
+
Iftwofieldsfrom`reducers`and`extraReducers`happentoendupwiththesameactiontypestring, the function from `reducers` will be used to handle that action type.
- custom action creator functions that have a `.match` property that is a type guard
50
50
@@ -56,7 +56,7 @@ Accepts the same inputs as `isAllOf` and will return a type guard function that
56
56
57
57
## `isAsyncThunkAction`
58
58
59
-
A higher-order function that returns a type guard function that may be used to check whether an action was created by [`createAsyncThunk`](./createAsyncThunk).
59
+
A higher-order function that returns a type guard function that may be used to check whether an action was created by [`createAsyncThunk`](./createAsyncThunk.mdx).
@@ -128,7 +128,7 @@ function handleRejectedAction(action: UnknownAction) {
128
128
129
129
## `isRejectedWithValue`
130
130
131
-
A higher-order function that returns a type guard function that may be used to check whether an action is a 'rejected' action creator from the `createAsyncThunk` promise lifecycle that was created by [`rejectWithValue`](./createAsyncThunk#handling-thunk-errors).
131
+
A higher-order function that returns a type guard function that may be used to check whether an action is a 'rejected' action creator from the `createAsyncThunk` promise lifecycle that was created by [`rejectWithValue`](./createAsyncThunk.mdx#handling-thunk-errors).
-`endpointName`: a string matching an existing endpoint name
164
167
-`args`: a cache key, used to determine which cached dataset needs to be updated
165
168
-`patches`: an array of patches (or inverse patches) to apply to cached state. These would typically be obtained from the result of dispatching [`updateQueryData`](#updatequerydata)
169
+
-`updateProvided`: a boolean indicating whether the endpoint's provided tags should be re-calculated based on the updated cache. Defaults to `false`.
166
170
167
171
#### Description
168
172
@@ -255,11 +259,21 @@ function selectInvalidatedBy(
255
259
Afunction that can select query parameters to be invalidated.
Copy file name to clipboardExpand all lines: docs/rtk-query/overview.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ import { createApi } from '@reduxjs/toolkit/query/react'
67
67
68
68
RTK Query includes these APIs:
69
69
70
-
-[`createApi()`](./api/createApi.mdx): The core of RTK Query's functionality. It allows you to define a set of endpointsdescribe how to retrieve data from a series of endpoints, including configuration of how to fetch and transform that data. In most cases, you should use this once per app, with "one API slice per base URL" as a rule of thumb.
70
+
-[`createApi()`](./api/createApi.mdx): The core of RTK Query's functionality. It allows you to define a set of "endpoints" that describe how to retrieve data from backend APIs and other async sources, including the configuration of how to fetch and transform that data. In most cases, you should use this once per app, with "one API slice per base URL" as a rule of thumb.
71
71
-[`fetchBaseQuery()`](./api/fetchBaseQuery.mdx): A small wrapper around [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) that aims to simplify requests. Intended as the recommended `baseQuery` to be used in `createApi` for the majority of users.
72
72
-[`<ApiProvider />`](./api/ApiProvider.mdx): Can be used as a `Provider` if you **do not already have a Redux store**.
73
73
-[`setupListeners()`](./api/setupListeners.mdx): A utility used to enable `refetchOnMount` and `refetchOnReconnect` behaviors.
Copy file name to clipboardExpand all lines: docs/rtk-query/usage/customizing-queries.mdx
+91-3Lines changed: 91 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,30 @@ See also [`baseQuery API Reference`](../api/createApi.mdx#basequery).
26
26
27
27
RTK Query expects a `baseQuery` function to be called with three arguments: `args`, `api`, and `extraOptions`. It is expected to return an object with either a `data` or `error` property, or a promise that resolves to return such an object.
28
28
29
+
:::tip
30
+
31
+
Base query and query functions must _always_ catch errors themselves, and return it in an object!
32
+
33
+
```ts no-transpile
34
+
function brokenCustomBaseQuery() {
35
+
// ❌ Don't let this throw by itself
36
+
const data =awaitfetchSomeData()
37
+
return { data }
38
+
}
39
+
40
+
function correctCustomBaseQuery() {
41
+
// ✅ Catch errors and _return_ them so the RTKQ logic can track it
42
+
try {
43
+
const data =awaitfetchSomeData()
44
+
return { data }
45
+
} catch (error) {
46
+
return { error }
47
+
}
48
+
}
49
+
```
50
+
51
+
:::
52
+
29
53
#### baseQuery function arguments
30
54
31
55
```ts title="baseQuery example arguments" no-transpile
Individual endpoints on [`createApi`](../api/createApi.mdx) accept a [`queryFn`](../api/createApi.mdx#queryfn) property which allows a given endpoint to ignore `baseQuery` for that endpoint by providing an inline function determining how that query resolves.
259
+
RTK Query comes with `fetchBaseQuery` out of the box, which makes it straightforward to define endpoints that talk to HTTP URLs (such as a typical REST API). We also have integrations with GraphQL as well. However, at its core, RTK Query is really about tracking loading state and cached values for _any_ async request/response sequence, not just HTTP requests.
236
260
237
-
This can be useful for scenarios where you want to have particularly different behaviour for a single endpoint, or where the query itself is not relevant. Such situations may include:
261
+
RTK Query supports defining endpoints that run arbitrary async logic and return a result. Individual endpoints on [`createApi`](../api/createApi.mdx) accept a [`queryFn`](../api/createApi.mdx#queryfn) property, which let you write your own async function with whatever logic you want inside.
262
+
263
+
This can be useful for scenarios where you want to have particularly different behaviour for a single endpoint, or where the query itself is not relevant, including:
238
264
239
265
- One-off queries that use a different base URL
240
266
- One-off queries that use different request handling, such as automatic re-tries
241
267
- One-off queries that use different error handling behaviour
268
+
- Queries that make requests using a third-party library SDK, such as Firebase or Supabase
269
+
- Queries that perform async tasks that are not a typical request/response
242
270
- Performing multiple requests with a single query ([example](#performing-multiple-requests-with-a-single-query))
243
271
- Leveraging invalidation behaviour with no relevant query ([example](#using-a-no-op-queryfn))
244
272
- Using [Streaming Updates](./streaming-updates) with no relevant initial request ([example](#streaming-data-with-no-initial-request))
@@ -247,7 +275,39 @@ See also [`queryFn API Reference`](../api/createApi.mdx#queryfn) for the type si
247
275
248
276
### Implementing a `queryFn`
249
277
250
-
In order to use `queryFn`, it can be treated as an inline `baseQuery`. It will be called with the same arguments as `baseQuery`, as well as the provided `baseQuery` function itself (`arg`, `api`, `extraOptions`, and `baseQuery`). Similarly to `baseQuery`, it is expected to return an object with either a `data` or `error` property, or a promise that resolves to return such an object.
278
+
A `queryFn` can be thought of as an inline `baseQuery`. It will be called with the same arguments as `baseQuery`, as well as the provided `baseQuery` function itself (`arg`, `api`, `extraOptions`, and `baseQuery`). Similarly to `baseQuery`, it is expected to return an object with either a `data` or `error` property, or a promise that resolves to return such an object.
You could also try creating a custom base query that uses the SDK, and define endpoints that pass method names or args into that base query.
968
+
881
969
### Using a no-op queryFn
882
970
883
971
In certain scenarios, you may wish to have a `query` or `mutation` where sending a request or returning data is not relevant for the situation. Such a scenario would be to leverage the `invalidatesTags` property to force re-fetch specific `tags` that have been provided to the cache.
0 commit comments