Skip to content

Commit

Permalink
docs: add mutate filter functions documentation in v2 (#340)
Browse files Browse the repository at this point in the history
* Empty commit for v2 branch

* docs: add mutate filter functions documentation in v2

* remove the usage of cache.clear() and use mutate instead
  • Loading branch information
koba04 authored Aug 24, 2022
1 parent 5647dac commit e81afb7
Show file tree
Hide file tree
Showing 13 changed files with 665 additions and 230 deletions.
19 changes: 14 additions & 5 deletions pages/docs/advanced/cache.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { Cache } from 'components/diagrams/cache'
When nested, SWR hooks will use the upper-level cache provider. If there is no upper-level cache provider, it fallbacks to the default cache provider, which is an empty `Map`.

<Callout emoji="โš ๏ธ">
If a cache provider is used, the global `mutate` will **not** work for SWR hooks under that `<SWRConfig>` boundary. Please use [this](#access-current-cache-provider) instead.
If a cache provider is used, the global `mutate` will **not** work for SWR hooks under that `<SWRConfig>` boundary. Please use [this](#access-current-cache-provider) instead.
</Callout>

## Access Current Cache Provider
Expand Down Expand Up @@ -187,13 +187,22 @@ describe('test suite', async () => {
})
```

### Access to the Cache
### Modify the Cache Data

Alert: you should not write to the cache directly, it might cause undefined behavior.
<Callout emoji="๐Ÿšจ" type="error">
You should not write to the cache directly, it might cause undefined behavior.
</Callout>

You can use [`mutate`](/docs/mutation) to modify the cache. For example, you can clear all cache data like the following.

```jsx
const { cache } = useSWRConfig()

cache.get(key) // Get the current data for a key.
cache.clear() // โš ๏ธ Clear all the cache. SWR will revalidate upon re-render.
mutate(
key => true, // which cache keys are updated
undefined, // update cache data to `undefined`
{ revalidate: false } // do not revalidate
)
```

More information can be found [here](/docs/arguments#multiple-arguments).
23 changes: 19 additions & 4 deletions pages/docs/advanced/cache.ja.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,28 @@ describe('test suite', async () => {
})
```

### ใ‚ญใƒฃใƒƒใ‚ทใƒฅใธใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹

่ญฆๅ‘Š๏ผšใ‚ญใƒฃใƒƒใ‚ทใƒฅใซ็›ดๆŽฅๆ›ธใ่พผใ‚€ใ“ใจใฏใ™ใ‚‹ในใใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไบˆๆœŸใ—ใชใ„ๅ‹•ไฝœใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚
### ใ‚ญใƒฃใƒƒใ‚ทใƒฅใ‚’ๆ›ดๆ–ฐใ™ใ‚‹

```jsx
const { cache } = useSWRConfig()

cache.get(key) // ใ‚ญใƒผใฎ็พๅœจใฎใƒ‡ใƒผใ‚ฟใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚
cache.clear() // โš ๏ธ ใ™ในใฆใฎใ‚ญใƒฃใƒƒใ‚ทใƒฅใ‚’ใ‚ฏใƒชใ‚ขใ—ใพใ™ใ€‚ SWRใฏใ€ๅ†ใƒฌใƒณใƒ€ใƒชใƒณใ‚ฐๆ™‚ใซๅ†ๆคœ่จผใ—ใพใ™ใ€‚
```

<Callout emoji="๐Ÿšจ" type="error">
ใ‚ญใƒฃใƒƒใ‚ทใƒฅใซ็›ดๆŽฅๆ›ธใ่พผใ‚€ใ“ใจใฏใ™ใ‚‹ในใใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไบˆๆœŸใ—ใชใ„ๅ‹•ไฝœใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚
</Callout>

[`mutate`](/docs/mutation) ใ‚’ใ‚ญใƒฃใƒƒใ‚ทใƒฅใ‚’ๆ›ดๆ–ฐใ™ใ‚‹ใŸใ‚ใซๅˆฉ็”จใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€ไธ‹่จ˜ใฎใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใงๅ…จใฆใฎใ‚ญใƒฃใƒƒใ‚ทใƒฅใƒ‡ใƒผใ‚ฟใ‚’ใ‚ฏใƒชใ‚ขใงใใพใ™ใ€‚

```jsx
const { cache } = useSWRConfig()

mutate(
key => true, // ใฉใฎใ‚ญใƒฃใƒƒใ‚ทใƒฅใ‚ญใƒผใ‚’ๆ›ดๆ–ฐใ™ใ‚‹ใ‹
undefined, // ใ‚ญใƒฃใƒƒใ‚ทใƒฅใƒ‡ใƒผใ‚ฟใ‚’ `undefined` ใซๆ›ดๆ–ฐใ™ใ‚‹
{ revalidate: false } // ๅ†ๆคœ่จผใ—ใชใ„
)
```

ๆ›ดใชใ‚‹ๆƒ…ๅ ฑใฏ [ใ“ใกใ‚‰](/docs/arguments#multiple-arguments) ใ‹ใ‚‰็ขบ่ชใงใใพใ™ใ€‚
23 changes: 19 additions & 4 deletions pages/docs/advanced/cache.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,28 @@ describe('test suite', async () => {
})
```

### ์บ์‹œ์— ์ ‘๊ทผํ•˜๊ธฐ

์•Œ๋ฆผ: ์บ์‹œ๋ฅผ ์ง์ ‘ ์“ฐ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ž‘์„ ์œ ๋ฐœํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
### Modify the Cache Data

```jsx
const { cache } = useSWRConfig()

cache.get(key) // ํ‚ค์— ๋Œ€ํ•œ ํ˜„์žฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ.
cache.clear() // โš ๏ธ ๋ชจ๋“  ์บ์‹œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ. SWR์€ ๋‹ค์‹œ ๋ Œ๋”๋งํ•  ๋•Œ ์žฌ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.
```

<Callout emoji="๐Ÿšจ" type="error">
์บ์‹œ๋ฅผ ์ง์ ‘ ์“ฐ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ž‘์„ ์œ ๋ฐœํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
</Callout>

You can use [`mutate`](/docs/mutation) to modify the cache. For example, you can clear all cache data like the following.

```jsx
const { cache } = useSWRConfig()

mutate(
key => true, // which cache keys are updated
undefined, // update cache data to `undefined`
{ revalidate: false } // do not revalidate
)
```

More information can be found [here](/docs/arguments#multiple-arguments).
Loading

0 comments on commit e81afb7

Please sign in to comment.