Skip to content

Commit 250790d

Browse files
committed
docs: more reorganizing and updates to docs
1 parent 999a5f4 commit 250790d

File tree

8 files changed

+161
-126
lines changed

8 files changed

+161
-126
lines changed

docs/docs/Features/defer-queries.md renamed to docs/docs/Features/defer-queries.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const loader = createLoader({
3030
});
3131
```
3232

33+
<img
34+
src={require("./deferred.png").default}
35+
alt="Deferred queries timeline"
36+
/>
37+
3338
## Configure
3439

3540
> **New in version 1.0.3**

docs/docs/Features/deferred.png

114 KB
Loading

docs/docs/Features/extending.md

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

docs/docs/Features/extending.mdx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
## Example
10+
11+
```tsx
12+
const parentLoader = createLoader({
13+
onLoading: () => <div>Loading...</div>
14+
});
15+
16+
const childLoader = parentLoader.extend({
17+
useQueries: () => ({...}),
18+
onError: () => <div>Error</div>,
19+
}).extend({
20+
transform: () => ({...}),
21+
}).extend({
22+
onLoading: () => <div>Overwritten loading...</div>,
23+
});
24+
```
25+
26+
## Separation of concerns
27+
28+
Its up to you how much you want to separate logic here. Some examples would be...
29+
30+
- Co-locating loaders in a shared folder
31+
- Co-locating loaders in same file as component
32+
- Co-locating loaders in same directory but in a separate file from the component
33+
34+
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.
35+
36+
## Creating a loader hierarchy
37+
38+
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.
39+
40+
<img
41+
src={require("../Quick Guide/extend-loader.png").default}
42+
alt="Loader hierarchy illustration"
43+
/>
44+
45+
## Tips
46+
47+
:::caution `.extend` will not merge what two separate `useQueries` returns.
48+
For example, you cannot _just_ inherit the deferredQueries, you must either inherit or overwrite the whole `useQueries` argument.
49+
:::
50+
51+
:::tip Reusable transformers
52+
You can extend as many times as you'd like. You can use this feature to easily inject reusable snippets, like transformers.
53+
54+
```typescript
55+
type QueryRecord = Record<string, UseQueryResult<unknown>>;
56+
57+
export const transformData = {
58+
transform: (data: {
59+
queries: QueryRecord;
60+
deferredQueries: QueryRecord;
61+
payload: unknown;
62+
}) => {
63+
// handle transformation in generic way
64+
},
65+
};
66+
```
67+
68+
```typescript
69+
const loader = createLoader({...}).extend(transformData);
70+
```
71+
72+
:::
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Passing arguments
2+
3+
You can setup loaders so that they can be used with arguments. How you pass the arguments depends on how you use the loader.
4+
5+
You set this up using the `queriesArg` argument. This is a function that takes the consumer's expected props and returns the argument that should be passed to the useQueries hook.
6+
7+
```tsx
8+
const loader = createLoader({
9+
queriesArg: (props: { userId: string }) => props.userId,
10+
useQueries: (userId) => {
11+
// userId is automatically inferred as string
12+
const user = useGetUserQuery(userId);
13+
14+
return {
15+
queries: {
16+
user,
17+
},
18+
};
19+
},
20+
});
21+
```
22+
23+
## Data flow
24+
25+
The transformation layer between the useQueries hook and the loader is the `queriesArg` function.
26+
27+
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 through withLoader.
28+
29+
<img
30+
src={require("../Quick Guide/queriesArg.png").default}
31+
alt="queriesArg"
32+
style={{ maxWidth: "100%" }}
33+
/>
34+
35+
## When using `<AwaitLoader />`
36+
37+
You should pass the expected _props_ to the `arg` prop when using `<AwaitLoader />`.
38+
39+
```tsx
40+
<AwaitLoader
41+
loader={loader}
42+
render={(data) => (...)}
43+
args={{
44+
userId: "1234",
45+
}}
46+
/>
47+
```
48+
49+
## Properly typing the `queriesArg` function
50+
51+
You can use the `ConsumerProps<T>` utility type to type the `queriesArg` function.
52+
53+
```tsx
54+
import {
55+
ConsumerProps,
56+
createLoader,
57+
} from "@ryfylke-react/rtk-query-loader";
58+
59+
type UserRouteLoaderProps = ConsumerProps<{
60+
userId: string;
61+
}>;
62+
63+
const loader = createLoader({
64+
queriesArg: (props: UserRouteLoaderProps) => props.userId,
65+
// ...
66+
});
67+
```
68+
69+
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:
70+
71+
```ts
72+
// This is valid ✅
73+
type Props_1 = {
74+
userId: string;
75+
};
76+
77+
// This is also valid ✅
78+
type Props_2 = {
79+
userId: string;
80+
someOtherProp: string;
81+
};
82+
```

docs/docs/Features/transforming.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ sidebar_position: 2
66

77
You can transform the queries to any format you'd like.
88

9+
## Example
10+
911
```ts
1012
const notTransformed = createLoader({
1113
useQueries: () => ({

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,62 +31,3 @@ export const userRouteLoader = baseLoader.extend({
3131
},
3232
});
3333
```
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-
```

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,3 @@ export const userRouteLoader = baseLoader.extend({});
1313
```
1414

1515
You can pass any argument from [`createLoader`](/Reference/create-loader) into [`Loader.extend`](/Features/extending).
16-
17-
:::info Separation of concerns
18-
Its up to you how much you want to separate logic here. Some examples would be...
19-
20-
- Co-locating loaders in a shared folder
21-
- Co-locating loaders in same file as component
22-
- Co-locating loaders in same directory but in a separate file from the component
23-
24-
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.
25-
:::
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)