Skip to content

Commit

Permalink
feat: useCreateLoader
Browse files Browse the repository at this point in the history
chore: added example to alt libs in docs
chore: added more exports from local types
  • Loading branch information
imp-dance committed Aug 19, 2023
1 parent 250790d commit 509050d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/docs/Features/other-libs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Although `rtk-query-loader` was build with `@reduxjs/toolkit` in mind, the under

## Tanstack Query

[See example on CodeSandbox](https://codesandbox.io/s/tanstack-query-rtk-query-loader-example-6393w2)

```typescript
import {
useQuery,
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ export type {
ComponentWithLoaderData,
ConsumerProps,
CreateUseLoaderArgs,
CreateLoaderArgs,
DeferredConfig,
LoaderConfig,
_TDeferred,
_TPayload,
_TQueries,
_TProps,
_TReturn,
MakeDataRequired,
CustomLoaderProps,
DataShapeInput,
InferLoaderData,
Expand All @@ -19,3 +28,4 @@ export type {
} from "./types";
export { withLoader } from "./withLoader";
export { AwaitLoader } from "./AwaitLoader";
export { useCreateLoader } from "./useCreateLoader";
9 changes: 6 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,12 @@ export type CreateUseLoaderArgs<
...args: OptionalGenericArg<TArg>
) => DataShapeInput<TQueries, TDeferred, TPayload>;
/** Transforms the output of the queries */
transform?: (
data: ResolveLoadedDataShape<TQueries, TDeferred, TPayload>
) => TReturn;
transform?: LoaderTransformFunction<
TQueries,
TDeferred,
TPayload,
TReturn
>;
config?: LoaderConfig;
};

Expand Down
31 changes: 31 additions & 0 deletions src/useCreateLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from "react";
import * as Types from "./types";
import { createLoader } from "./createLoader";

/**
* Allows you to create a loader inside of a component.
* This is useful if you want to create a loader for use with `AwaitLoader`, scoped to a component.
*/
export const useCreateLoader = <
TProps extends unknown,
TQueries extends Types._TQueries,
TDeferred extends Types._TDeferred,
TPayload extends Types._TPayload,
TReturn extends unknown = Types.ResolveDataShape<
Types.MakeDataRequired<TQueries>,
TDeferred,
TPayload
>,
TArg extends unknown = never
>(
createLoaderArgs: Types.CreateLoaderArgs<
TProps,
TQueries,
TDeferred,
TPayload,
TReturn,
TArg
>
) => {
return React.useRef(createLoader(createLoaderArgs)).current;
};

0 comments on commit 509050d

Please sign in to comment.