-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
354 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
sidebar_position: 7 | ||
--- | ||
|
||
# AwaitLoader | ||
|
||
AwaitLoader is a component that consumes a loader and then renders the proper load/fetch/error states, and `props.render` on success. | ||
|
||
## Arguments | ||
|
||
- **`loader`** - The loader that contains the queries you want to await. | ||
- **`render`** - A function that recieves the loader data and should return a ReactElement. | ||
- **`args`** - Takes in the expected props for the given loader. | ||
|
||
```tsx | ||
const loader = createLoader({ | ||
queriesArg: (props: { name: string }) => props.name, | ||
useQueries: (name) => ({ | ||
queries: { | ||
pokemon: useGetPokemonQuery(name), | ||
}, | ||
}), | ||
}); | ||
|
||
const App = () => { | ||
return ( | ||
<div> | ||
<AwaitLoader | ||
loader={loader} | ||
args={{ name: "charizard" }} | ||
render={(data) => ( | ||
<pre>{JSON.stringify(data.queries.pokemon.data)}</pre> | ||
)} | ||
/> | ||
<AwaitLoader | ||
loader={loader} | ||
args={{ name: "pikachu" }} | ||
render={(data) => ( | ||
<pre>{JSON.stringify(data.queries.pokemon.data)}</pre> | ||
)} | ||
/> | ||
</div> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as React from "react"; | ||
import * as Types from "./types"; | ||
import { withLoader } from "./withLoader"; | ||
type ReactType = typeof React; | ||
let R = React; | ||
|
||
type AwaitLoaderProps< | ||
TProps extends Record<string, any>, | ||
TReturn, | ||
TArg = never | ||
> = [TArg] extends [never] | ||
? { | ||
loader: Types.Loader<TProps, TReturn, any, any, any, TArg>; | ||
render: (data: TReturn) => React.ReactElement; | ||
} | ||
: { | ||
loader: Types.Loader<TProps, TReturn, any, any, any, TArg>; | ||
render: (data: TReturn) => React.ReactElement; | ||
args: TProps; | ||
}; | ||
|
||
export const AwaitLoader = < | ||
TProps extends Record<string, any>, | ||
TReturn extends unknown, | ||
TArg = never | ||
>( | ||
args: AwaitLoaderProps<TProps, TReturn, TArg> | ||
) => { | ||
const Component = R.useCallback( | ||
withLoader( | ||
(_, loaderData) => args.render(loaderData), | ||
args.loader | ||
), | ||
[] | ||
); | ||
return R.createElement( | ||
Component, | ||
"args" in args ? args.args : ({} as TProps), | ||
null | ||
); | ||
}; | ||
|
||
export const _testLoad = (react: any) => { | ||
R = react as ReactType; | ||
return AwaitLoader; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.