Skip to content

Commit 0aded32

Browse files
committed
Add RTKQ examples page
1 parent 6af65d1 commit 0aded32

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

docs/usage/rtk-query/examples.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
id: examples
3+
title: RTK Query Examples
4+
sidebar_label: Examples
5+
hide_title: true
6+
---
7+
8+
# RTK Query Examples
9+
10+
## Examples Overview
11+
12+
We have a variety of examples that demonstrate various aspects of using RTK Query. The examples were created as part of the standalone `@rtk-incubator/rtk-query` development cycle. We're currently working to update them as part of the process of finalizing RTK Query's integration into Redux Toolkit, so the imports are mismatched and the examples are not currently in the RTK repo. However, you should be able to inspect these examples and use the logic they show as guidelines.
13+
14+
These examples are not meant to be what you base your application on, but exist to show _very specific_ behaviors that you may not actually want or need in your application. For most users, the basic examples in the [Queries](./queries) and [Mutations](./mutations) sections will cover the majority of your needs.
15+
16+
Please note that when playing with the examples in CodeSandbox that you can experience quirky behavior, especially if you fork them and start editing files. Hot reloading, CSB service workers and [`msw`](https://mswjs.io/) sometimes have trouble getting on the right page -- when that happens, just refresh in the CSB browser pane.
17+
18+
## React Hooks
19+
20+
<iframe src="https://codesandbox.io/embed/rtk-query-demo-lbp7n?fontsize=12&hidenavigation=1&theme=dark&runonclick=1"
21+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
22+
title="rtk-query-react-hooks-example"
23+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
24+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
25+
></iframe>
26+
27+
## React Optimistic Updates
28+
29+
In the example below you'll notice a few things. There are two `Posts` list on the sidebar. The top one will only update _after_ a successful mutation and resync with the server. The _subscribed_ one will update immediately due to the optimistic update. In the event of an error, you'll see this get rolled back.
30+
31+
:::info
32+
The example has some intentionally wonky behavior... when editing the name of a post, there is a decent chance you'll get a random error.
33+
:::
34+
35+
<iframe src="https://codesandbox.io/embed/concepts-optimistic-updates-lush8?fontsize=12&hidenavigation=1&module=%2Fsrc%2Fapp%2Fservices%2Fposts.ts&theme=dark&runonclick=1"
36+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
37+
title="RTK Query Optimistic Update Example"
38+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
39+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
40+
></iframe>
41+
42+
## React with GraphQL
43+
44+
<iframe src="https://codesandbox.io/embed/rtk-query-graphql-basic-zkqfz?fontsize=12&hidenavigation=1&theme=dark&module=%2Fsrc%2Fapi.js&runonclick=1"
45+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
46+
title="RTK Query GraphQL Example"
47+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
48+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
49+
></iframe>
50+
51+
## Authentication
52+
53+
There are several ways to handle authentication with RTK Query. This is a very basic example of taking a JWT from a login mutation, then setting that in our store. We then use `prepareHeaders` to inject the authentication headers into every subsequent request.
54+
55+
### Dispatching an action to set the user state
56+
57+
This example dispatches a `setCredentials` action to store the user and token information.
58+
59+
<iframe src="https://codesandbox.io/embed/rtk-query-authentication-example-zx8me?fontsize=12&hidenavigation=1&module=%2Fsrc%2Ffeatures%2Fauth%2FauthSlice.tsx&theme=dark&runonclick=1"
60+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
61+
title="RTK Query Authentication Example"
62+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
63+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
64+
></iframe>
65+
66+
### Using `extraReducers`
67+
68+
This example uses a matcher from the endpoint and `extraReducers` in the `authSlice`.
69+
70+
<iframe src="https://codesandbox.io/embed/rtk-query-authentication-example-extrareducers-5w7q9?fontsize=12&hidenavigation=1&module=%2Fsrc%2Ffeatures%2Fauth%2FauthSlice.tsx&theme=dark&runonclick=1"
71+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
72+
title="RTK Query Authentication Example - extraReducers"
73+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
74+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
75+
></iframe>
76+
77+
## React Class Components
78+
79+
Check out the `PostDetail` component for an example of Class Component usage.
80+
81+
<iframe src="https://codesandbox.io/embed/react-class-component-example-48hyq?fontsize=12&hidenavigation=1&theme=dark&runonclick=1"
82+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
83+
title="rtk-query-react-hooks-example"
84+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
85+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
86+
></iframe>
87+
88+
## Svelte
89+
90+
<iframe src="https://codesandbox.io/embed/svelte-app-rtk-simplequery-demo-f1ueb?fontsize=12&hidenavigation=1&theme=dark&runonclick=1"
91+
style={{ width: '100%', height: '800px', border: 0, borderRadius: '4px', overflow: 'hidden' }}
92+
title="rtk-query-svelte-example"
93+
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
94+
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
95+
></iframe>

website/sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"usage/rtk-query/optimistic-updates",
4141
"usage/rtk-query/code-splitting",
4242
"usage/rtk-query/code-generation",
43-
"usage/rtk-query/customizing-create-api"
43+
"usage/rtk-query/customizing-create-api",
44+
"usage/rtk-query/examples"
4445
]
4546
}
4647
]

0 commit comments

Comments
 (0)