|
1 | 1 | # Upgrading
|
2 | 2 |
|
| 3 | +## Upgrade to v11 |
| 4 | + |
| 5 | +### promiseFn and deferFn unification |
| 6 | + |
| 7 | +The `promiseFn` and the `deferFn` have been unified. They now share the following signature: |
| 8 | + |
| 9 | +```ts |
| 10 | +export type AsyncFn<T, C> = ( |
| 11 | + context: C | undefined, |
| 12 | + props: AsyncProps<T, C>, |
| 13 | + controller: AbortController |
| 14 | +) => Promise<T> |
| 15 | +``` |
| 16 | +
|
| 17 | +Before the `deferFn` and `promiseFn` had this signature: |
| 18 | +
|
| 19 | +```ts |
| 20 | +export type PromiseFn<T> = (props: AsyncProps<T>, controller: AbortController) => Promise<T> |
| 21 | + |
| 22 | +export type DeferFn<T> = ( |
| 23 | + args: any[], |
| 24 | + props: AsyncProps<T>, |
| 25 | + controller: AbortController |
| 26 | +) => Promise<T> |
| 27 | +``` |
| 28 | +
|
| 29 | +The difference is the idea of having a `context`, the context will contain all parameters |
| 30 | +to `AsyncProps` which are not native to the `AsyncProps`. Before you could pass any parameter |
| 31 | +to `AsyncProps` and it would pass them to the `deferFn` and `promiseFn`, now you need to use |
| 32 | +the `context` instead. |
| 33 | +
|
| 34 | +For example before you could write: |
| 35 | +
|
| 36 | +```jsx |
| 37 | +useAsync({ promiseFn: loadPlayer, playerId: 1 }) |
| 38 | +``` |
| 39 | +
|
| 40 | +Now you must write: |
| 41 | +
|
| 42 | +```jsx |
| 43 | +useAsync({ promiseFn: loadPlayer, context: { playerId: 1 }}) |
| 44 | +``` |
| 45 | +
|
| 46 | +In the above example the context would be `{playerId: 1}`. |
| 47 | +
|
| 48 | +This means that `promiseFn` now expects three parameters instead of two. |
| 49 | +
|
| 50 | +So before in `< 10.0.0` you would do this: |
| 51 | +
|
| 52 | +```jsx |
| 53 | +import { useAsync } from "react-async" |
| 54 | + |
| 55 | +// Here loadPlayer has only two arguments |
| 56 | +const loadPlayer = async (options, controller) => { |
| 57 | + const res = await fetch(`/api/players/${options.playerId}`, { signal: controller.signal }) |
| 58 | + if (!res.ok) throw new Error(res.statusText) |
| 59 | + return res.json() |
| 60 | +} |
| 61 | + |
| 62 | +// With hooks |
| 63 | +const MyComponent = () => { |
| 64 | + const state = useAsync({ promiseFn: loadPlayer, playerId: 1 }) |
| 65 | +} |
| 66 | + |
| 67 | +// With the Async component |
| 68 | +<Async promiseFn={loadPlayer} playerId={1} /> |
| 69 | +``` |
| 70 | +
|
| 71 | +In `11.0.0` you need to account for the three parameters: |
| 72 | +
|
| 73 | +```jsx |
| 74 | +import { useAsync } from "react-async" |
| 75 | + |
| 76 | +// Now it has three arguments |
| 77 | +const loadPlayer = async (context, options, controller) => { |
| 78 | + const res = await fetch(`/api/players/${context.playerId}`, { signal: controller.signal }) |
| 79 | + if (!res.ok) throw new Error(res.statusText) |
| 80 | + return res.json() |
| 81 | +} |
| 82 | + |
| 83 | +// With hooks |
| 84 | +const MyComponent = () => { |
| 85 | + const state = useAsync({ promiseFn: loadPlayer, context: { playerId: 1 } }) |
| 86 | +} |
| 87 | + |
| 88 | +// With the Async component |
| 89 | +<Async promiseFn={loadPlayer} context={{ playerId: 1 }} /> |
| 90 | +``` |
| 91 | +
|
| 92 | +For the `deferFn` this means no longer expecting an array of arguments but instead a singular argument. |
| 93 | +The `run` now accepts only one argument which is a singular value. All other arguments to `run` but |
| 94 | +the first will be ignored. |
| 95 | +
|
| 96 | +So before in `< 10.0.0` you would do this: |
| 97 | +
|
| 98 | +```jsx |
| 99 | +import Async from "react-async" |
| 100 | + |
| 101 | +const getAttendance = () => |
| 102 | + fetch("/attendance").then( |
| 103 | + () => true, |
| 104 | + () => false |
| 105 | + ) |
| 106 | +const updateAttendance = ([attend, userId]) => |
| 107 | + fetch(`/attendance/${userId}`, { method: attend ? "POST" : "DELETE" }).then( |
| 108 | + () => attend, |
| 109 | + () => !attend |
| 110 | + ) |
| 111 | + |
| 112 | +const userId = 42 |
| 113 | + |
| 114 | +const AttendanceToggle = () => ( |
| 115 | + <Async promiseFn={getAttendance} deferFn={updateAttendance}> |
| 116 | + {({ isPending, data: isAttending, run, setData }) => ( |
| 117 | + <Toggle |
| 118 | + on={isAttending} |
| 119 | + onClick={() => { |
| 120 | + run(!isAttending, userId) |
| 121 | + }} |
| 122 | + disabled={isPending} |
| 123 | + /> |
| 124 | + )} |
| 125 | + </Async> |
| 126 | +) |
| 127 | +``` |
| 128 | +
|
| 129 | +In `11.0.0` you need to account for for the parameters not being an array: |
| 130 | +
|
| 131 | +```jsx |
| 132 | +import Async from "react-async" |
| 133 | + |
| 134 | +const getAttendance = () => |
| 135 | + fetch("/attendance").then( |
| 136 | + () => true, |
| 137 | + () => false |
| 138 | + ) |
| 139 | +const updateAttendance = ({ attend, userId }) => |
| 140 | + fetch(`/attendance/${userId}`, { method: attend ? "POST" : "DELETE" }).then( |
| 141 | + () => attend, |
| 142 | + () => !attend |
| 143 | + ) |
| 144 | + |
| 145 | +const userId = 42 |
| 146 | + |
| 147 | +const AttendanceToggle = () => ( |
| 148 | + <Async promiseFn={getAttendance} deferFn={updateAttendance}> |
| 149 | + {({ isPending, data: isAttending, run, setData }) => ( |
| 150 | + <Toggle |
| 151 | + on={isAttending} |
| 152 | + onClick={() => { |
| 153 | + run({ attend: isAttending, userId }) |
| 154 | + }} |
| 155 | + disabled={isPending} |
| 156 | + /> |
| 157 | + )} |
| 158 | + </Async> |
| 159 | +) |
| 160 | +``` |
| 161 | +
|
| 162 | +### useAsync only accepts one prop |
| 163 | +
|
| 164 | +Before in `10.0.0` you could call useAsync with multiple parameters, |
| 165 | +the first argument would then be the `promiseFn` like this: |
| 166 | +
|
| 167 | +```tsx |
| 168 | +const state = useAsync(loadPlayer, { context: { playerId: 1 } }) |
| 169 | +``` |
| 170 | +
|
| 171 | +In `11.0.0` there is only one parameter. So the overload no longer works and you need to write this instead: |
| 172 | +
|
| 173 | +```tsx |
| 174 | +const state = useAsync({ promiseFn: loadPlayer, context: { playerId: 1 } }) |
| 175 | +``` |
| 176 | +
|
| 177 | +### WatchFn |
| 178 | +
|
| 179 | +Another thing you need to be careful about is the `watchFn` you can no longer count on the fact that |
| 180 | +unknown parameters are put into the `AsyncProps`. Before `< 10.0.0` you would write: |
| 181 | +
|
| 182 | +```ts |
| 183 | +useAsync({ |
| 184 | + promiseFn, |
| 185 | + count: 0, |
| 186 | + watchFn: (props, prevProps) => props.count !== prevProps.count |
| 187 | +}); |
| 188 | +``` |
| 189 | +
|
| 190 | +In `11.0.0` you need to use the `context` instead: |
| 191 | +
|
| 192 | +```ts |
| 193 | +useAsync({ |
| 194 | + promiseFn, |
| 195 | + context: { count: 0 }, |
| 196 | + watchFn: (props, prevProps) => props.context.count !== prevProps.context.count |
| 197 | +}); |
| 198 | +``` |
| 199 | +
|
| 200 | +## Upgrade to v10 |
| 201 | +
|
| 202 | +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. |
| 203 | +
|
3 | 204 | ## Upgrade to v9
|
4 | 205 |
|
5 | 206 | The rejection value for failed requests with `useFetch` was changed. Previously it was the Response object. Now it's an
|
|
0 commit comments