Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add revalidate in the documentation #70

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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