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