Skip to content

Commit 999a5f4

Browse files
committed
docs: Large update to documentation, added illustrations, refactoring content
1 parent f27570a commit 999a5f4

24 files changed

+184
-172
lines changed

docs/docs/Advanced/index.mdx

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/docs/Advanced/under-the-hood.md

Lines changed: 0 additions & 72 deletions
This file was deleted.

docs/docs/Features/_category_.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

docs/docs/Features/other-libs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar_position: 4
77
You can use RTK Query Loader with most other similar query-fetching libraries. This is possible through the use of _resolvers_.
88

99
:::note
10-
Although `rtk-query-loader` was build with `@reduxjs/toolkit` in mind, the underlying principles can be applied to any similar data loading solution. [Read more about how RTK Query Loader works under the hood](../Advanced/under-the-hood.md).
10+
Although `rtk-query-loader` was build with `@reduxjs/toolkit` in mind, the underlying principles can be applied to any similar data loading solution.
1111
:::
1212

1313
## Tanstack Query
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"collapsed": false
3+
}

docs/docs/Quick Guide/accept-arguments.md

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# 4. Arguments for your loader
6+
7+
If you want the loader to take an argument, all you have to do is to add a `queriesArg`:
8+
9+
```tsx {5-7,10-12}
10+
import { ConsumerProps } from "@ryfylke-react/rtk-query-loader";
11+
12+
// This means that any component that has props that extend this
13+
// type can consume the loader using `withLoader`
14+
type UserRouteLoaderProps = ConsumerProps<{
15+
userId: string;
16+
}>;
17+
18+
export const userRouteLoader = baseLoader.extend({
19+
queriesArg: (props: UserRouteLoaderProps) => props.userId,
20+
// type is now inferred from queriesArg return
21+
useQueries: (userId) => {
22+
const user = useGetUserQuery(userId);
23+
const posts = useGetPostsByUser(userId);
24+
25+
return {
26+
queries: {
27+
user,
28+
posts,
29+
},
30+
};
31+
},
32+
});
33+
```
34+
35+
## Data flow
36+
37+
The transformation layer between the useQueries hook and the loader is the `queriesArg` function. It takes the component's props and returns the argument that should be passed to the useQueries hook. This is nessecary to be able to consume the loader in a type-safe way.
38+
39+
<img
40+
src={require("./queriesArg.png").default}
41+
alt="queriesArg"
42+
style={{ maxWidth: "100%" }}
43+
/>
44+
45+
## When using `<AwaitLoader />`
46+
47+
You should pass the expected _props_ to the `arg` prop when using `<AwaitLoader />`.
48+
49+
```tsx
50+
<AwaitLoader
51+
loader={loader}
52+
render={(data) => (...)}
53+
args={{
54+
userId: "1234",
55+
}}
56+
/>
57+
```
58+
59+
## Properly typing the `queriesArg` function
60+
61+
You can use the `ConsumerProps<T>` utility type to type the `queriesArg` function.
62+
63+
```tsx
64+
import {
65+
ConsumerProps,
66+
createLoader,
67+
} from "@ryfylke-react/rtk-query-loader";
68+
69+
type UserRouteLoaderProps = ConsumerProps<{
70+
userId: string;
71+
}>;
72+
73+
const loader = createLoader({
74+
queriesArg: (props: UserRouteLoaderProps) => props.userId,
75+
// ...
76+
});
77+
```
78+
79+
The type utility will ensure that the type can be extended by the consumer components. This means that the following types are both valid for the definition above:
80+
81+
```ts
82+
// This is valid ✅
83+
type Props_1 = {
84+
userId: string;
85+
};
86+
87+
// This is also valid ✅
88+
type Props_2 = {
89+
userId: string;
90+
someOtherProp: string;
91+
};
92+
```
File renamed without changes.

docs/docs/Quick Guide/consume-loader.mdx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@ import TabItem from "@theme/TabItem";
77

88
# 5. Consume the loader
99

10-
You can consume a loader using one of the following methods:
10+
## Picking the right method
11+
12+
We recommend consuming the loader with either `withLoader` or `AwaitLoader`.
13+
14+
- Use `withLoader` if you want to use the loader to affect the whole component.
15+
- Use `AwaitLoader` if you want to use the loader in a more conditional way, or if you only want a small section of your component to start loading data, after the rest of the component has rendered.
16+
17+
You could also use `useLoader`, which is the hook that `withLoader` and `AwaitLoader` use under the hood. This will simply return the aggregated queries of the loader, and is useful if you want to use the data, but **not the loading and error states of the loader**.
18+
19+
<img
20+
src={require("./rtk-query-loader-chart.png").default}
21+
alt="Chart showing differences between AwaitLoader and withLoader"
22+
style={{ maxWidth: 500 }}
23+
/>
24+
25+
## Examples
26+
27+
Select a tab below to see an example of how you would consume the loader using that specific method.
1128

1229
<Tabs groupId="method" queryString>
1330
<Tab label="withLoader" value="withLoader" default>
@@ -62,6 +79,8 @@ export const UserRoute = withLoader((props: Props, loader) => {
6279

6380
<Tab label="<AwaitLoader>" value="load">
6481

82+
> **New in version 1.0.4**
83+
6584
If you prefer not using higher order components, or want to use the loader in a more conditional way, you can use the &lt;`AwaitLoader` /&gt; component.
6685

6786
```tsx {6-18}

docs/docs/Quick Guide/extend-loader.md renamed to docs/docs/Quick Guide/extend-loader.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ Its up to you how much you want to separate logic here. Some examples would be..
2323

2424
I personally prefer to keep the loaders close to the component, either in a file besides it or directly in the file itself, and then keep a base loader somewhere else to extend from.
2525
:::
26+
27+
## Loader hierarchy
28+
29+
You can extend from any loader, including loaders that have already been extended. This allows you to create a hierarchy of loaders that can be used to share logic between components.
30+
31+
<img
32+
src={require("./extend-loader.png").default}
33+
alt="Loader hierarchy illustration"
34+
/>

0 commit comments

Comments
 (0)