vercel/swr
### [`v1.3.0`](https://togithub.com/vercel/swr/releases/tag/1.3.0)
[Compare Source](https://togithub.com/vercel/swr/compare/1.2.2...1.3.0)
#### What's Changed
- type: fix type error on SWRConfig by [@Himself65](https://togithub.com/Himself65) in [#1913](https://togithub.com/vercel/swr/issues/1913)
- chore: update React 18 dependencies by [@shuding](https://togithub.com/shuding) in [#1824](https://togithub.com/vercel/swr/issues/1824)
- test: fix an act warning by [@koba04](https://togithub.com/koba04) in [#1888](https://togithub.com/vercel/swr/issues/1888)
- feat: support functional optimisticData by [@huozhi](https://togithub.com/huozhi) in [#1861](https://togithub.com/vercel/swr/issues/1861)
- bugfix: make suspense and revalidateIfStale work together by [@simowe](https://togithub.com/simowe) in [#1851](https://togithub.com/vercel/swr/issues/1851)
**Full Changelog**: https://github.com/vercel/swr/compare/1.2.2...1.3.0
### [`v1.2.2`](https://togithub.com/vercel/swr/releases/tag/1.2.2)
[Compare Source](https://togithub.com/vercel/swr/compare/1.2.1...1.2.2)
#### Highlights of This Release
##### `populateCache` Option Now Supports Function
We added better Optimistic UI support in [v1.2.0](https://togithub.com/vercel/swr/releases/tag/1.2.0). However, what if your API is only returning a subset of the data (such as the mutated part), that can be populated into the cache? Usually, an extra revalidation after that mutation is needed. But now you can also use a function as `populateCache` to transform the mutate result into the full data:
```js
await mutate(addTodo(newTodo), {
optimisticData: [...data, newTodo],
rollbackOnError: true,
populateCache: (addedTodo, currentData) => {
// `addedTodo` is what the API returns. It's not
// returning a list of all current todos but only
// the new added one.
// In this case, we can transform the mutate result
// together with current data, into the new data
// that can be updated.
return [...currentData, addedTodo];
},
// Since the API already gives us the updated information,
// we don't need to revalidate here.
revalidate: false,
});
```
The new definition:
```ts
populateCache?: boolean | ((mutationResult: any, currentData: Data) => Data)
```
Here is a demo for it: https://codesandbox.io/s/swr-basic-forked-hi9svh
##### Bug Fixes
#### What's Changed
- refactor: revalidateIfStale has an effect on updates, not only mounting by [@koba04](https://togithub.com/koba04) in [https://github.com/vercel/swr/pull/1837](https://togithub.com/vercel/swr/pull/1837)
- fix: reset stale unmountedRef in suspense by [@promer94](https://togithub.com/promer94) in [https://github.com/vercel/swr/pull/1843](https://togithub.com/vercel/swr/pull/1843)
- test: add a test for the behavior of revalidateOnMount when the key has been changed by [@koba04](https://togithub.com/koba04) in [https://github.com/vercel/swr/pull/1847](https://togithub.com/vercel/swr/pull/1847)
- feat: Support `populateCache` as a function by [@shuding](https://togithub.com/shuding) in [https://github.com/vercel/swr/pull/1818](https://togithub.com/vercel/swr/pull/1818)
**Full Changelog**: https://github.com/vercel/swr/compare/1.2.1...1.2.2
Configuration
📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).
🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.
♻ Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.
🔕 Ignore: Close this PR and you won't be reminded about this update again.
[ ] If you want to rebase/retry this PR, check this box
This PR has been generated by Mend Renovate. View repository job log here.
This PR contains the following updates:
1.2.1
->1.3.0
Release Notes
vercel/swr
### [`v1.3.0`](https://togithub.com/vercel/swr/releases/tag/1.3.0) [Compare Source](https://togithub.com/vercel/swr/compare/1.2.2...1.3.0) #### What's Changed - type: fix type error on SWRConfig by [@Himself65](https://togithub.com/Himself65) in [#1913](https://togithub.com/vercel/swr/issues/1913) - chore: update React 18 dependencies by [@shuding](https://togithub.com/shuding) in [#1824](https://togithub.com/vercel/swr/issues/1824) - test: fix an act warning by [@koba04](https://togithub.com/koba04) in [#1888](https://togithub.com/vercel/swr/issues/1888) - feat: support functional optimisticData by [@huozhi](https://togithub.com/huozhi) in [#1861](https://togithub.com/vercel/swr/issues/1861) - bugfix: make suspense and revalidateIfStale work together by [@simowe](https://togithub.com/simowe) in [#1851](https://togithub.com/vercel/swr/issues/1851) **Full Changelog**: https://github.com/vercel/swr/compare/1.2.2...1.3.0 ### [`v1.2.2`](https://togithub.com/vercel/swr/releases/tag/1.2.2) [Compare Source](https://togithub.com/vercel/swr/compare/1.2.1...1.2.2) #### Highlights of This Release ##### `populateCache` Option Now Supports Function We added better Optimistic UI support in [v1.2.0](https://togithub.com/vercel/swr/releases/tag/1.2.0). However, what if your API is only returning a subset of the data (such as the mutated part), that can be populated into the cache? Usually, an extra revalidation after that mutation is needed. But now you can also use a function as `populateCache` to transform the mutate result into the full data: ```js await mutate(addTodo(newTodo), { optimisticData: [...data, newTodo], rollbackOnError: true, populateCache: (addedTodo, currentData) => { // `addedTodo` is what the API returns. It's not // returning a list of all current todos but only // the new added one. // In this case, we can transform the mutate result // together with current data, into the new data // that can be updated. return [...currentData, addedTodo]; }, // Since the API already gives us the updated information, // we don't need to revalidate here. revalidate: false, }); ``` The new definition: ```ts populateCache?: boolean | ((mutationResult: any, currentData: Data) => Data) ``` Here is a demo for it: https://codesandbox.io/s/swr-basic-forked-hi9svh ##### Bug Fixes #### What's Changed - refactor: revalidateIfStale has an effect on updates, not only mounting by [@koba04](https://togithub.com/koba04) in [https://github.com/vercel/swr/pull/1837](https://togithub.com/vercel/swr/pull/1837) - fix: reset stale unmountedRef in suspense by [@promer94](https://togithub.com/promer94) in [https://github.com/vercel/swr/pull/1843](https://togithub.com/vercel/swr/pull/1843) - test: add a test for the behavior of revalidateOnMount when the key has been changed by [@koba04](https://togithub.com/koba04) in [https://github.com/vercel/swr/pull/1847](https://togithub.com/vercel/swr/pull/1847) - feat: Support `populateCache` as a function by [@shuding](https://togithub.com/shuding) in [https://github.com/vercel/swr/pull/1818](https://togithub.com/vercel/swr/pull/1818) **Full Changelog**: https://github.com/vercel/swr/compare/1.2.1...1.2.2Configuration
📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).
🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.
♻ Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.
🔕 Ignore: Close this PR and you won't be reminded about this update again.
This PR has been generated by Mend Renovate. View repository job log here.