|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +import TOCInline from "@theme/TOCInline"; |
| 6 | + |
| 7 | +# Examples |
| 8 | + |
| 9 | +This page contains a collection of examples that cover typical use cases for the library. |
| 10 | + |
| 11 | +<TOCInline toc={toc} /> |
| 12 | + |
| 13 | +## React Router (route loader) |
| 14 | + |
| 15 | +By utilizing `useParams` from `react-router-dom` we can create a loader that will load data based on the route parameters. |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { |
| 19 | + withLoader, |
| 20 | + createLoader, |
| 21 | +} from "@ryfylke-react/rtk-query-loader"; |
| 22 | +import { useParams, Redirect } from "react-router-dom"; |
| 23 | +// ... |
| 24 | + |
| 25 | +const userRouteLoader = createLoader({ |
| 26 | + useQueries: () => { |
| 27 | + const { userId } = useParams(); |
| 28 | + const userQuery = useGetUserQuery(userId, { |
| 29 | + skip: userId ? false : true, |
| 30 | + }); |
| 31 | + |
| 32 | + return { |
| 33 | + queries: { |
| 34 | + userQuery, |
| 35 | + }, |
| 36 | + }; |
| 37 | + }, |
| 38 | + onLoading: (props) => { |
| 39 | + const { userId } = useParams(); |
| 40 | + if (!userId) { |
| 41 | + return <Redirect to="/users" />; |
| 42 | + } |
| 43 | + return <PageSkeleton />; |
| 44 | + }, |
| 45 | + onError: (props, error) => <ErrorView error={error} />, |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +## Reusable loader |
| 50 | + |
| 51 | +We can also create a reusable loader that can be used with multiple components. |
| 52 | + |
| 53 | +```tsx |
| 54 | +import { |
| 55 | + withLoader, |
| 56 | + createLoader, |
| 57 | + ConsumerProps, |
| 58 | +} from "@ryfylke-react/rtk-query-loader"; |
| 59 | + |
| 60 | +type UserLoaderProps = { |
| 61 | + userId: string; |
| 62 | +}; |
| 63 | + |
| 64 | +const userLoader = createLoader<UserLoaderProps>({ |
| 65 | + queriesArg: (props: UserLoaderProps) => props.userId, |
| 66 | + useQueries: (userId) => { |
| 67 | + const userQuery = useGetUserQuery(userId); |
| 68 | + |
| 69 | + return { |
| 70 | + queries: { |
| 71 | + userQuery, |
| 72 | + }, |
| 73 | + }; |
| 74 | + }, |
| 75 | + onLoading: (props) => <PageSkeleton />, |
| 76 | + onError: (props, error) => <ErrorView error={error} />, |
| 77 | +}); |
| 78 | +``` |
| 79 | + |
| 80 | +You can now use the `userLoader` in any component whos props extend `UserLoaderProps`. |
| 81 | + |
| 82 | +### Consumer 1 |
| 83 | + |
| 84 | +```tsx title="UserProfile.tsx" |
| 85 | +import { userLoader } from "../loaders"; |
| 86 | + |
| 87 | +type UserProfileProps = { |
| 88 | + userId: string; |
| 89 | + // ... other props |
| 90 | +}; |
| 91 | + |
| 92 | +export const UserProfile = withLoader( |
| 93 | + (props: UserProfileProps, data) => { |
| 94 | + return <>...</>; |
| 95 | + }, |
| 96 | + userLoader |
| 97 | +); |
| 98 | +``` |
| 99 | + |
| 100 | +### Consumer 2 |
| 101 | + |
| 102 | +```tsx title="UserProfile.tsx" |
| 103 | +import { userLoader } from "../loaders"; |
| 104 | + |
| 105 | +type InlineUserDetailsProps = { |
| 106 | + userId: string; |
| 107 | + dir: "row" | "column"; |
| 108 | + // ... other props |
| 109 | +}; |
| 110 | + |
| 111 | +export const InlineUserDetails = withLoader( |
| 112 | + (props: InlineUserDetailsProps, data) => { |
| 113 | + return <>...</>; |
| 114 | + }, |
| 115 | + userLoader |
| 116 | +); |
| 117 | +``` |
| 118 | + |
| 119 | +## Stateful loader |
| 120 | + |
| 121 | +You can also create loaders that contain state. |
| 122 | + |
| 123 | +```tsx |
| 124 | +const loader = createLoader({ |
| 125 | + useQueries: () => { |
| 126 | + const [name, setName] = useState("charizard"); |
| 127 | + const debouncedName = useDebounce(name, 200); |
| 128 | + const pokemon = useGetPokemon(debouncedName); |
| 129 | + return { |
| 130 | + queries: { |
| 131 | + pokemon, |
| 132 | + }, |
| 133 | + payload: { |
| 134 | + name, |
| 135 | + setName, |
| 136 | + }, |
| 137 | + }; |
| 138 | + }, |
| 139 | +}); |
| 140 | + |
| 141 | +const Consumer = withLoader((props, data) => { |
| 142 | + return ( |
| 143 | + <div> |
| 144 | + <input |
| 145 | + value={data.payload.name} |
| 146 | + onChange={(e) => data.payload.setName(e.target.value)} |
| 147 | + /> |
| 148 | + <div>AP: {data.queries.pokemon.data.ability_power}</div> |
| 149 | + </div> |
| 150 | + ); |
| 151 | +}, loader); |
| 152 | +``` |
0 commit comments