|
1 | 1 | # Upgrading
|
2 | 2 |
|
| 3 | +## Upgrade to v11 |
| 4 | + |
| 5 | +The `promiseFn` and the `deferFn` have been unified. They now share the following signature: |
| 6 | + |
| 7 | +```ts |
| 8 | +export type AsyncFn<T, C> = ( |
| 9 | + context: C | undefined, |
| 10 | + props: AsyncProps<T, C>, |
| 11 | + controller: AbortController |
| 12 | +) => Promise<T> |
| 13 | +``` |
| 14 | +
|
| 15 | +Before the `deferFn` and `promiseFn` had this signature: |
| 16 | +
|
| 17 | +```ts |
| 18 | +export type PromiseFn<T> = (props: AsyncProps<T>, controller: AbortController) => Promise<T> |
| 19 | + |
| 20 | +export type DeferFn<T> = ( |
| 21 | + args: any[], |
| 22 | + props: AsyncProps<T>, |
| 23 | + controller: AbortController |
| 24 | +) => Promise<T> |
| 25 | +``` |
| 26 | +
|
| 27 | +The difference is the idea of having a `context`, the context will contain all parameters |
| 28 | +to `AsyncProps` which are not native to the `AsyncProps`. For example: |
| 29 | +
|
| 30 | +```jsx |
| 31 | +useAsync({ promiseFn: loadPlayer, playerId: 1 }) |
| 32 | +``` |
| 33 | +
|
| 34 | +In the above example the context would be `{playerId: 1}`. |
| 35 | +
|
| 36 | +This means that you know need to expect three parameter for the `promiseFn` instead of two. |
| 37 | +
|
| 38 | +So before in `< 10.0.0` you would do this: |
| 39 | +
|
| 40 | +```jsx |
| 41 | +import { useAsync } from "react-async" |
| 42 | + |
| 43 | +// Here loadPlayer has only two arguments |
| 44 | +const loadPlayer = async (options, controller) => { |
| 45 | + const res = await fetch(`/api/players/${options.playerId}`, { signal: controller.signal }) |
| 46 | + if (!res.ok) throw new Error(res.statusText) |
| 47 | + return res.json() |
| 48 | +} |
| 49 | + |
| 50 | +const MyComponent = () => { |
| 51 | + const { data, error, isPending } = useAsync({ promiseFn: loadPlayer, playerId: 1 }) |
| 52 | +} |
| 53 | +``` |
| 54 | +
|
| 55 | +In `11.0.0` you need to account for the three parameters: |
| 56 | +
|
| 57 | +```jsx |
| 58 | +import { useAsync } from "react-async" |
| 59 | + |
| 60 | +// With two arguments: |
| 61 | +const loadPlayer = async (context, options, controller) => { |
| 62 | + const res = await fetch(`/api/players/${context.playerId}`, { signal: controller.signal }) |
| 63 | + if (!res.ok) throw new Error(res.statusText) |
| 64 | + return res.json() |
| 65 | +} |
| 66 | + |
| 67 | +const MyComponent = () => { |
| 68 | + // You can either pass arguments by adding extra keys to the AsyncProps |
| 69 | + const { data, error, isPending } = useAsync({ promiseFn: loadPlayer, playerId: 1 }) |
| 70 | + |
| 71 | + // Or you can explicitly define the context which is TypeScript friendly |
| 72 | + const { data, error, isPending } = useAsync({ promiseFn: loadPlayer, context: { playerId: 1 } }) |
| 73 | +} |
| 74 | +``` |
| 75 | +
|
| 76 | +For the `deferFn` this means no longer expecting an array of arguments but instead a singular argument. |
| 77 | +The `run` now accepts only one argument which is a singular value. All other arguments to `run` but |
| 78 | +the first will be ignored. |
| 79 | +
|
| 80 | +So before in `< 10.0.0` you would do this: |
| 81 | +
|
| 82 | +```jsx |
| 83 | +import Async from "react-async" |
| 84 | + |
| 85 | +const getAttendance = () => |
| 86 | + fetch("/attendance").then( |
| 87 | + () => true, |
| 88 | + () => false |
| 89 | + ) |
| 90 | +const updateAttendance = ([attend, userId]) => |
| 91 | + fetch(`/attendance/${userId}`, { method: attend ? "POST" : "DELETE" }).then( |
| 92 | + () => attend, |
| 93 | + () => !attend |
| 94 | + ) |
| 95 | + |
| 96 | +const userId = 42 |
| 97 | + |
| 98 | +const AttendanceToggle = () => ( |
| 99 | + <Async promiseFn={getAttendance} deferFn={updateAttendance}> |
| 100 | + {({ isPending, data: isAttending, run, setData }) => ( |
| 101 | + <Toggle |
| 102 | + on={isAttending} |
| 103 | + onClick={() => { |
| 104 | + run(!isAttending, userId) |
| 105 | + }} |
| 106 | + disabled={isPending} |
| 107 | + /> |
| 108 | + )} |
| 109 | + </Async> |
| 110 | +) |
| 111 | +``` |
| 112 | +
|
| 113 | +In `11.0.0` you need to account for for the parameters not being an array: |
| 114 | +
|
| 115 | +```jsx |
| 116 | +import Async from "react-async" |
| 117 | + |
| 118 | +const getAttendance = () => |
| 119 | + fetch("/attendance").then( |
| 120 | + () => true, |
| 121 | + () => false |
| 122 | + ) |
| 123 | +const updateAttendance = ({ attend, userId }) => |
| 124 | + fetch(`/attendance/${userId}`, { method: attend ? "POST" : "DELETE" }).then( |
| 125 | + () => attend, |
| 126 | + () => !attend |
| 127 | + ) |
| 128 | + |
| 129 | +const userId = 42 |
| 130 | + |
| 131 | +const AttendanceToggle = () => ( |
| 132 | + <Async promiseFn={getAttendance} deferFn={updateAttendance}> |
| 133 | + {({ isPending, data: isAttending, run, setData }) => ( |
| 134 | + <Toggle |
| 135 | + on={isAttending} |
| 136 | + onClick={() => { |
| 137 | + run({ attend: isAttending, userId }) |
| 138 | + }} |
| 139 | + disabled={isPending} |
| 140 | + /> |
| 141 | + )} |
| 142 | + </Async> |
| 143 | +) |
| 144 | +``` |
| 145 | +
|
| 146 | +## Upgrade to v10 |
| 147 | +
|
| 148 | +This is a major release due to the migration to TypeScript. While technically it shouldn't change anything, it might be a breaking change in certain situations. Theres also a bugfix for watchFn and a fix for legacy browsers. |
| 149 | +
|
3 | 150 | ## Upgrade to v9
|
4 | 151 |
|
5 | 152 | The rejection value for failed requests with `useFetch` was changed. Previously it was the Response object. Now it's an
|
|
0 commit comments