|
| 1 | +# Migrating to `1.0.0` |
| 2 | + |
| 3 | +If you are using `@ryfylke-react/rtk-query-loader@0.3.51` or earlier, these docs will help you migrate your existing codebase over to `1.0.0`. |
| 4 | + |
| 5 | +## Input format for data |
| 6 | + |
| 7 | +Previously, queries have been passed using two arguments: |
| 8 | + |
| 9 | +- **`queries`** |
| 10 | +- **`deferredQueries`** |
| 11 | + |
| 12 | +These two functions used to return a `readonly UseQueryResult<T>[]`. |
| 13 | + |
| 14 | +In version `1.0.0`, these arguments have now been joined into one, called `useQueries`. |
| 15 | + |
| 16 | +```typescript {3-10,15-23} |
| 17 | +// Previously |
| 18 | +const loader = createLoader({ |
| 19 | + queries: (arg: string) => { |
| 20 | + const pokemonQuery = useGetPokemon(arg); |
| 21 | + return [pokemonQuery] as const; |
| 22 | + }, |
| 23 | + deferredQueries: () => { |
| 24 | + const otherQuery = useOtherQuery(); |
| 25 | + return [otherQuery] as const; |
| 26 | + }, |
| 27 | +}); |
| 28 | + |
| 29 | +// In version 1: |
| 30 | +const loader = createLoader({ |
| 31 | + useQueries: (arg: string) => { |
| 32 | + const pokemonQuery = useGetPokemon(arg); |
| 33 | + const otherQuery = useOtherQuery(); |
| 34 | + |
| 35 | + return { |
| 36 | + queries: { pokemonQuery }, |
| 37 | + deferredQueries: { otherQuery }, |
| 38 | + }; |
| 39 | + }, |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +:::info |
| 44 | +Previously, you _had_ to use `transform` as well to expose the deferred queries to your consumer. This is no longer required. |
| 45 | +::: |
| 46 | + |
| 47 | +## Output format for data |
| 48 | + |
| 49 | +This also means that the output format of the loader has changed. Typescript will help you out here, but for the most part **what you send in is what you get out**. |
| 50 | + |
| 51 | +```typescript |
| 52 | +const loader = createLoader({ |
| 53 | + useQueries: () => ({ |
| 54 | + queries: { |
| 55 | + pokemons: useGetPokemons(), |
| 56 | + }, |
| 57 | + }), |
| 58 | +}); |
| 59 | + |
| 60 | +type LoaderData = InferLoaderData<typeof loader>; |
| 61 | +// { |
| 62 | +// queries: { |
| 63 | +// pokemons: UseQueryResult<Pokemon[]> |
| 64 | +// } |
| 65 | +// } |
| 66 | +``` |
| 67 | + |
| 68 | +This output format change will affect your consumer components, as well as the (now single) argument passed to `transform`. |
| 69 | + |
| 70 | +You can still of course optionally [transform](../Features/transforming.md) the output. |
| 71 | + |
| 72 | +:::tip You should know |
| 73 | +This I/O interface change is the **only breaking change** from the previous versions. |
| 74 | +::: |
| 75 | + |
| 76 | +:::note |
| 77 | +Through rewriting the tests and lots of local testing, I can assure you that this refactor should be relatively straight forward and easy to do. If you fix the loaders first by changing and `queries` and `deferredQueries` to now use `useQueries`, you can let Typescript help you out in the consumers afterwards. |
| 78 | +::: |
| 79 | + |
| 80 | +## New feature: `payload` |
| 81 | + |
| 82 | +You can now send _any_ data you want through the loader. This is useful for when your loader contains a lot of logic, or if you want to pass some handlers down to the consumer to change the arguments of your queries. |
| 83 | + |
| 84 | +```typescript |
| 85 | +const loader = createLoader({ |
| 86 | + useQueries: () => { |
| 87 | + const [pokemonName, setPokemonName] = useState("charizard"); |
| 88 | + const pokemon = useGetPokemon(pokemonName); |
| 89 | + |
| 90 | + return { |
| 91 | + queries: { |
| 92 | + pokemon, |
| 93 | + }, |
| 94 | + payload: { |
| 95 | + changePokemon: setPokemonName, |
| 96 | + }, |
| 97 | + }; |
| 98 | + }, |
| 99 | +}); |
| 100 | + |
| 101 | +const Consumer = withLoader((props, data) => { |
| 102 | + const { payload, queries } = data; |
| 103 | + // ... |
| 104 | +}, loader); |
| 105 | +``` |
| 106 | + |
| 107 | +Previously, if you wanted to do something like this, you had to use [useCreateQuery](../Exports/use-create-query.md), or a wrapper component. If you did, you can now refactor your code to use `payload` instead, which should be a lot more clean and flexible. |
| 108 | + |
| 109 | +## Reporting bugs |
| 110 | + |
| 111 | +If you find any bugs or issues with this new update, I strongly encourage you to [file a bug report](https://github.com/ryfylke-react-as/rtk-query-loader/issues/new?assignees=&labels=&template=bug_report.md&title=) on the Github repo. |
| 112 | + |
| 113 | +I hope you are happy with the changes coming in version `1`. They have been thoroughly thought out and brewed on for a couple of months. Although it's never fun to push a breaking change, I hope you agree that these changes add a lot more flexibility to the loaders, and less confusion around I/O format. I'm personally very glad that I'm no longer required to rembember to use `as const` to ensure I get the correct types in my loader. |
0 commit comments