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
|**queries**? |`(arg?: T) => readonly UseQueryResults[]`| Returns a `readonly` array of useQuery results. **This function is a hook**, which means that you can use other hooks, like `useLocation`, in it. |
115
-
|**transform**? |`(queries: readonly UseQueryResult[], deferredQueries: readonly UseQueryResult[]) => T`| Transforms the list of queries to the desired loader output format. (Is also given deferredQueries as second argument) |
116
-
|**queriesArg**? |`(props: T) => A`| Creates an argument for the queries function based on expected props. Useful when you have queries in your loader that need arguments from the props of the component. |
117
-
|**deferredQueries**? |`(arg?: T) => readonly UseQueryResults[]`| Queries that should not affect the component-state (loading/error/success). Component might render before these are fulfilled. Like `queries`, **this function is a hook**, which means that you can use other hooks, like `useLocation`, in it. |
|**onFetching**? |`(props: T, Component: (() => ReactElement)) => ReactElement`| Make sure you call the second argument as a component, not a function. |
121
-
|**whileFetching**? |`{ append?: (props: P, data?: R) => ReactElement; prepend?: (props: P, data?: R) => ReactElement; }`| By using this instead of `onFetching`, you ensure that you don't reset the internal state of the component while fetching. |
122
-
123
-
## withLoader
124
-
125
-
Wraps a component to provide it with loader data.
126
-
127
-
```tsx
128
-
const postsLoader =createLoader(...);
129
-
130
-
const Component =withLoader(
131
-
(props:Props, loaderData) => {
132
-
// Can safely assume that loaderData and props are populated.
133
-
const posts =loaderData.posts;
134
-
135
-
returnposts.map(,,,);
136
-
},
137
-
postsLoader
138
-
)
139
-
140
-
```
141
-
142
-
#### Arguments
143
-
144
-
1.`(props: P, loaderData: R) => ReactElement`
145
-
Component with loader-data
146
-
2.`Loader`
147
-
Return value of `createLoader`.
148
-
149
-
## createUseLoader
150
-
151
-
Creates only the hook for the loader, without the extra metadata like loading state.
152
-
153
-
Basically just joins multiple queries into one, and optionally transforms the output. Returns a standard RTK useQuery hook.
154
-
155
-
A good solution for when you want more control over what happens during the lifecycle of the query.
156
-
157
-
```tsx
158
-
const useLoader =createUseLoader({
159
-
queries: (arg:string) =>
160
-
[
161
-
useQuery(arg.query),
162
-
useOtherQuery(arg.otherQuery),
163
-
] asconst,
164
-
transform: (queries) => ({
165
-
query: queries[0].data,
166
-
otherQuery: queries[1].data,
167
-
}),
168
-
});
169
-
170
-
const Component = () => {
171
-
const query =useLoader();
172
-
173
-
if (query.isLoading) {
174
-
return <div>loading...</div>;
175
-
}
176
-
//...
177
-
};
178
-
```
179
-
180
-
## Deferring queries
181
-
182
-
You can defer queries by using the `deferredQueries` argument in `createLoader` (or `createUseLoader`). These queries are passed as the second argument to `transform` which has to be used to access the deferred queries in your loaded component.
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. You either overwrite both or none of these.
238
-
239
-
* Supplying _just_ a new `queries` argument will result in `transform` being `undefined` in practise.
240
-
* Supplying _just_ a new `transform` argument will result in the new transform being ignored.
241
-
* Supplying a new `transform` _and_ a new `queries` argument will properly overwrite the existing base properties.
242
-
243
-
All other properties in the loader will overwrite as expected. You can, for example, just supply a new `onLoading`, or `onError`.
0 commit comments