Skip to content

Commit

Permalink
docs: add revalidate in the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Jan 25, 2021
1 parent de287dd commit 3dca0c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
30 changes: 30 additions & 0 deletions pages/docs/mutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,36 @@ function App () {
}
```

You can also use the `revalidate` function that `useSWR` returns.

```jsx
import useSWR from 'swr'

function App () {
const { data, revalidate } = useSWR('/api/user', fetcher);
return (
<div>
<Profile />
<button onClick={() => {
// set the cookie as expired
document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'

// tell all SWRs with this key to revalidate
revalidate()
}}>
Logout
</button>
</div>
)
}
```

`revalidate` accepts `retryCount` and `dedupe` as an option argument.

```jsx
revalidate({ retryCount, dedupe })
```

## Mutation and POST Request

In many cases, applying local mutations to data is a good way to make changes
Expand Down
11 changes: 6 additions & 5 deletions pages/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)

## Parameters

- `key`: a unique key string for the request (or a function / array / null) [(advanced usage)](/docs/conditional-fetching)
- `fetcher`: (_optional_) a Promise returning function to fetch your data [(details)](/docs/data-fetching)
- `key`: a unique key string for the request (or a function / array / null) [(advanced usage)](/docs/conditional-fetching)
- `fetcher`: (_optional_) a Promise returning function to fetch your data [(details)](/docs/data-fetching)
- `options`: (_optional_) an object of options for this SWR hook

## Return Values
- `data`: data for the given key resolved by `fetcher` (or undefined if not loaded)
- `error`: error thrown by `fetcher` (or undefined)
- `isValidating`: if there's a request or revalidation loading
- `data`: data for the given key resolved by `fetcher` (or undefined if not loaded)
- `error`: error thrown by `fetcher` (or undefined)
- `isValidating`: if there's a request or revalidation loading
- `mutate(data?, shouldRevalidate?)`: function to mutate the cached data
- `revalidate({ retryCount, dedupe }?)`: function to revalidate the data

## Options

Expand Down

0 comments on commit 3dca0c0

Please sign in to comment.