Skip to content

Commit 3ed7aa5

Browse files
committed
version: 1.0.7
1 parent 9acbad7 commit 3ed7aa5

File tree

8 files changed

+443
-195
lines changed

8 files changed

+443
-195
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ryfylke-react/rtk-query-loader",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Lets you create reusable, extendable RTK loaders for React components.",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

src/AwaitLoader.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ type AwaitLoaderProps<
1919
args: TProps;
2020
};
2121

22+
/**
23+
* @typedef AwaitLoaderProps
24+
* @type {Object}
25+
* @property {Types.Loader<TProps, TReturn, any, any, any, TArg>} loader The loader to use.
26+
* @property {(data: TReturn) => React.ReactElement} render The render function to use.
27+
* @property {TProps} args The arguments to pass to the loader.
28+
*/
29+
30+
/**
31+
* A component that awaits a loader and renders the data.
32+
* @param {AwaitLoaderProps} args The arguments to pass to the loader.
33+
*/
2234
export const AwaitLoader = <
2335
TProps extends Record<string, any>,
2436
TReturn extends unknown,

src/RTKLoader.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { SerializedError } from "@reduxjs/toolkit";
22
import * as React from "react";
33
import { CustomLoaderProps } from "./types";
44

5+
/**
6+
* The default loader component for use with RTK Query Loader.
7+
*/
58
export function RTKLoader<T>(
69
props: CustomLoaderProps<T>
710
): React.ReactElement {

src/createLoader.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ export const createUseLoader = <
9797
return useLoader;
9898
};
9999

100+
/**
101+
* Creates a `loader` that can be used to fetch data and render error & loading states.
102+
* @example
103+
* const loader = createLoader({
104+
* queriesArg: (props) => props.userId,
105+
* useQueries: (userId) => {
106+
* const user = useGetUserQuery(userId);
107+
* return { queries: { user } };
108+
* },
109+
* onError: (error) => <ErrorView error={error} />,
110+
* onLoading: () => <LoadingView />,
111+
* });
112+
*/
100113
export const createLoader = <
101114
TProps extends unknown,
102115
TQueries extends Types._TQueries,

src/createQuery.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ const requestIdGenerator = () => {
1212

1313
/**
1414
* Creates a query from an async getter function.
15-
*
16-
* ```ts
15+
* @param getter The async function to get the data.
16+
* @param dependencies The dependency array to watch for changes.
17+
* @example
1718
* const query = useCreateQuery(async () => {
18-
* const response = await fetch("https://example.com");
19+
* const response = await fetch(`/users/${userId}`);
1920
* return response.json();
20-
* });
21-
* ```
21+
* }, [userId]);
2222
*/
2323
export const useCreateQuery = <T extends unknown>(
2424
getter: Types.CreateQueryGetter<T>,
2525
dependencies?: any[]
2626
): Types.UseQueryResult<T> => {
2727
const safeDependencies = dependencies ?? [];
28-
const requestId = R.useRef(requestIdGenerator()).current;
28+
const [requestId] = R.useState(() => requestIdGenerator());
2929
const [state, dispatch] = R.useReducer(reducer, {
3030
isLoading: true,
3131
isSuccess: false,

src/types.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,15 @@ export type CreateLoaderArgs<
274274
/** Generates an argument for the `queries` based on component props */
275275
queriesArg?: (props: TProps) => TArg;
276276
/** Determines what to render while loading (with no data to fallback on) */
277-
onLoading?: (props: TProps) => ReactElement;
277+
onLoading?: (
278+
props: TProps,
279+
joinedQuery: UseQueryResult<TReturn>
280+
) => ReactElement;
278281
/** Determines what to render when query fails. */
279282
onError?: (
280283
props: TProps,
281284
error: FetchBaseQueryError | SerializedError,
282-
joinedQuery: UseQueryResult<undefined>
285+
joinedQuery: UseQueryResult<TReturn>
283286
) => ReactElement;
284287
/** @deprecated Using onFetching might result in loss of internal state. Use `whileFetching` instead, or pass the query to the component */
285288
onFetching?: (
@@ -337,12 +340,15 @@ export type Loader<
337340
/** Generates an argument for the `queries` based on component props */
338341
queriesArg?: (props: TProps) => TArg;
339342
/** Determines what to render while loading (with no data to fallback on) */
340-
onLoading?: (props: TProps) => ReactElement;
343+
onLoading?: (
344+
props: TProps,
345+
joinedQuery: UseQueryResult<TReturn>
346+
) => ReactElement;
341347
/** Determines what to render when query fails. */
342348
onError?: (
343349
props: TProps,
344350
error: SerializedError | FetchBaseQueryError,
345-
joinedQuery: UseQueryResult<undefined>
351+
joinedQuery: UseQueryResult<TReturn>
346352
) => ReactElement;
347353
/** @deprecated Using onFetching might result in loss of internal state. Use `whileFetching` instead, or pass the query to the component */
348354
onFetching?: (
@@ -352,7 +358,17 @@ export type Loader<
352358
/** Determines what to render besides success-result while query is fetching. */
353359
whileFetching?: WhileFetchingArgs<TProps, TReturn>;
354360
config?: LoaderConfig;
355-
/** Returns a new `Loader` extended from this `Loader`, with given overrides. */
361+
/**
362+
* Creates a `loader` that can be used to fetch data and render error & loading states.
363+
* @example
364+
* const loader = baseLoader.extend({
365+
* queriesArg: (props) => props.userId,
366+
* useQueries: (userId) => {
367+
* const user = useGetUserQuery(userId);
368+
* return { queries: { user } };
369+
* },
370+
* });
371+
*/
356372
extend: <
357373
E_TQueries extends _TQueries = TQueries,
358374
E_TDeferred extends _TDeferred = TDeferred,

src/withLoader.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
33
import * as React from "react";
44
import * as Types from "./types";
55

6+
/**
7+
* A higher order component that wraps a component and provides it with a loader.
8+
* @param Component The component to wrap with a loader. Second argument is the resolved loader data.
9+
* @param loader The loader to use.
10+
* @returns A component that will load the data and pass it to the wrapped component.
11+
* @example
12+
* const Component = withLoader((props, loaderData) => {
13+
* return <div>{loaderData.queries.user.name}</div>;
14+
* }, loader);
15+
*/
616
export const withLoader = <
717
TProps extends Record<string, any>,
818
TReturn extends unknown,
@@ -48,16 +58,12 @@ export const withLoader = <
4858
>;
4959
}
5060

51-
const onLoading = loader.onLoading?.(props);
61+
const onLoading = loader.onLoading?.(props, query);
5262

5363
const onError = loader.onError
5464
? (error: SerializedError | FetchBaseQueryError) => {
5565
if (!loader.onError) return <React.Fragment />;
56-
return loader.onError(
57-
props,
58-
error,
59-
query as Types.UseQueryResult<undefined>
60-
);
66+
return loader.onError(props, error, query);
6167
}
6268
: undefined;
6369

0 commit comments

Comments
 (0)