Skip to content

Commit 39d71f4

Browse files
committed
docs: updated docs
1 parent 892c190 commit 39d71f4

File tree

12 files changed

+194
-7
lines changed

12 files changed

+194
-7
lines changed

docs/docs/Advanced/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
sidebar_position: 6
3+
---
4+
5+
# Advanced
6+
7+
Read more about how `rtk-query-loader` actually works.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Under the hood
2+
3+
How does `rtk-query-loader` work _under the hood_?
4+
5+
## Virtual DOM
6+
7+
Imagine you have this component, rendered through `withLoader`:
8+
9+
```tsx
10+
const loader = createLoader({...});
11+
12+
const Component = withLoader((props, queries) => {...}, loader);
13+
14+
const App = () => {
15+
return (
16+
<div>
17+
<Component />
18+
</div>
19+
)
20+
}
21+
```
22+
23+
What actually ends up rendering in the virtual DOM is something like this:
24+
25+
```txt title="onLoading"
26+
div
27+
└── RTKLoader
28+
└── loader.onLoading(props)
29+
```
30+
31+
```txt title="onError"
32+
div
33+
└── RTKLoader
34+
└── loader.onError(props, error)
35+
```
36+
37+
```txt title="onFetching"
38+
div
39+
└── RTKLoader
40+
└── loader.onFetching(props, Component)
41+
```
42+
43+
```txt title="onSuccess"
44+
div
45+
└── RTKLoader
46+
└── null
47+
└── Component (with data)
48+
└── null
49+
```
50+
51+
```txt title="whileFetching"
52+
div
53+
└── RTKLoader
54+
└── loader.whileFetching.prepend()
55+
└── Component (with data)
56+
└── loader.whileFetching.append()
57+
```
58+
59+
## RTKLoader
60+
61+
`RTKLoader` simply receives the loader and component, and handles returning the correct state depending on the query. You can take a look at how this component behaves [in the codebase](https://github.com/ryfylke-react-as/rtk-query-loader/blob/main/src/RTKLoader.tsx).
62+
63+
## Custom `loaderComponent`
64+
65+
You could pass a custom `loaderComponent` to your loaders, if you'd like:
66+
67+
```typescript
68+
const CustomLoader = (props: CustomLoaderProps) => {
69+
// Handle rendering
70+
};
71+
72+
const loader = createLoader({
73+
loaderComponent: CustomLoader,
74+
// ...
75+
});
76+
```
77+
78+
This allows you to have really fine control over how the rendering of components using `withLoader` should work.

docs/docs/Exports/create-loader.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,38 @@ sidebar_position: 1
66

77
Creates a `Loader`.
88

9-
Takes the following argument:
9+
```typescript title="example.ts"
10+
type ConsumerProps = Record<string, any> & {
11+
userId: string;
12+
};
13+
14+
const loader = createLoader({
15+
queriesArg: (props: ConsumerProps) => props.userId,
16+
queries: (userId) => {
17+
return [useGetUser(userId)] as const;
18+
},
19+
deferredQueries: (userId) => {
20+
return [useGetUserRelations(userId)] as const;
21+
},
22+
transform: (queries, deferredQueries) => ({
23+
queries: [...queries, ...deferredQueries],
24+
foo: "bar",
25+
}),
26+
onLoading: (props) => <div>Loading...</div>,
27+
onError: (props, error) => <div>Error!</div>,
28+
whileFetching: {
29+
prepend: (props) => <LoadingSpinner />,
30+
},
31+
});
32+
```
33+
34+
A `Loader` takes ownership over...
35+
36+
- Data-loading
37+
- Data-transformation
38+
- Fetch-state rendering
39+
40+
Here is the `createLoader` argument:
1041

1142
````typescript
1243
type CreateLoaderArgs<

docs/docs/Exports/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 5
33
---
44

55
# Exports

docs/docs/Exports/use-create-query.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ const loader = createLoader({
2626
},
2727
});
2828
```
29+
30+
> You lose some great features from RTK query when using `useCreateQuery`.
31+
> If possible, try to stick to using actual queries, created from a `@reduxjs/toolkit` API.
32+
> You can look at this feature like an escape-hatch that allows you to pass other
33+
> data through the loader as well.

docs/docs/Features/extending.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Extend
6+
7+
You can **extend** existing `Loader`s. This lets you inherit and overwrite properties from an existing `Loader`.
8+
9+
```tsx
10+
const parentLoader = createLoader({
11+
onLoading: () => <div>Loading...</div>
12+
});
13+
14+
const childLoader = parentLoader.extend({
15+
queries: () => [...] as const,
16+
})
17+
```
18+
19+
It's worth mentioning that queries and transform are linked in this context, meaning that if you supply a new queries argument in the extended loader, but no transform, then you will not inherit the transform from the original loader.
20+
21+
- Supplying just a new `queries` argument will result in transform being undefined in practise.
22+
- Supplying just a new `transform` argument will result in the new transform being ignored.
23+
- Supplying a new `transform` and a new `queries` argument will properly overwrite the existing base properties.
24+
25+
All other properties in the loader will overwrite as expected. You can, for example, just supply a new `onLoading`, or `onError`.
26+
27+
> You may extend _extended_ loaders, to create an inheritance model.

docs/docs/Features/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# Features
6+
7+
Read more about various `rtk-query-loader` features.

docs/docs/Features/transforming.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Transform
6+
7+
You can transform the queries to any format you'd like.
8+
9+
```ts
10+
const notTransformed = createLoader({
11+
queries: () => [useGetPokemonsQuery()],
12+
});
13+
14+
type NotTransformedData = InferLoaderData<typeof notTransformed>;
15+
// readonly [UseQueryResults<Pokemon[]>]
16+
17+
const transformed = createLoader({
18+
queries: () => [useGetPokemonsQuery()],
19+
transform: (queries) => ({
20+
results: queries[0].data,
21+
query: queries[0],
22+
}),
23+
});
24+
25+
type TransformedData = InferLoaderData<typeof transformed>;
26+
// { results: Pokemon[]; query: UseQueryResult<Pokemon[]>; }
27+
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const UserRoute = withLoader((props: Props, queries) => {
3737

3838
Every `Loader` contains an actual hook that you can call to run all the queries and aggregate their statuses as if they were just one joined query.
3939

40-
```tsx
40+
```tsx {3,9}
4141
import { userRouteLoader } from "./baseLoader";
4242

4343
const useLoaderData = userRouteLoader.useLoader;

docs/docs/Quick Guide/extend-loader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { baseLoader } from "./baseLoader";
1212
export const userRouteLoader = baseLoader.extend({});
1313
```
1414

15-
You can pass any argument from [`createLoader`](/Exports/create-loader) into `Loader.extend`.
15+
You can pass any argument from [`createLoader`](/Exports/create-loader) into [`Loader.extend`](/Features/extending).
1616

1717
Its up to you how much you want to separate logic here. Some examples would be...
1818

0 commit comments

Comments
 (0)