-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetcher no longer accepts multiple arguments in v2 (#326)
* Fetcher no longer accepts multiple arguments in v2 * Empty commit for v2 branch * Apply suggestions from code review Co-authored-by: Jiachi Liu <inbox@huozhi.im> * Update pages/docs/arguments.en-US.md Co-authored-by: Jiachi Liu <inbox@huozhi.im> * apply suggestions to other languages Co-authored-by: Jiachi Liu <inbox@huozhi.im>
- Loading branch information
Showing
14 changed files
with
201 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,60 @@ | ||
# Argumentos | ||
|
||
Por padrão, `key` será passado para `fetcher` como argumento. Então as 3 expressões a seguir são equivalentes: | ||
|
||
```js | ||
useSWR('/api/user', () => fetcher('/api/user')) | ||
useSWR('/api/user', url => fetcher(url)) | ||
useSWR('/api/user', fetcher) | ||
``` | ||
|
||
## Múltiplos Argumentos | ||
|
||
Em alguns cenários, é útil passar vários argumentos (pode ser qualquer valor ou objeto) para a função `fetcher`. | ||
Por exemplo, uma requisição de fetch autorizada: | ||
|
||
```js | ||
useSWR('/api/user', url => fetchWithToken(url, token)) | ||
``` | ||
|
||
Isso é **incorreto**. Porque o identificador (também a cache key) do dado é `'/api/user'`, | ||
mesmo se `token` mudar, SWR ainda usará a mesma chave e retornará o dado errado. | ||
|
||
Ao invés disso, você pode usar um array como o parâmetro `key`, que contém vários argumentos para a função `fetcher`. | ||
|
||
```js | ||
const { data: user } = useSWR(['/api/user', token], fetchWithToken) | ||
``` | ||
|
||
A função `fetcherWithToken` ainda aceita os mesmos 2 argumentos, mas a cache key será associada com `token` agora. | ||
|
||
## Passando Objetos | ||
|
||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
<Callout> | ||
Desde a versão 1.1.0, chaves de objeto serão serializadas por baixo dos panos automaticamente. | ||
</Callout> | ||
|
||
Digamos que você tenha outra função que busca dados com um escopo de usuário: `fetchWithUser(api, user)`. Você pode fazer o seguinte: | ||
|
||
```js | ||
const { data: user } = useSWR(['/api/user', token], fetchWithToken) | ||
|
||
// ...e então passá-lo como argumento para outro hook useSWR | ||
const { data: orders } = useSWR(user ? ['/api/orders', user] : null, fetchWithUser) | ||
``` | ||
|
||
Você pode passar diretamente um objeto como chave, e `fetcher` receberá esse objeto também: | ||
|
||
```js | ||
const { data: orders } = useSWR({ url: '/api/orders', args: user }, fetcher) | ||
``` | ||
|
||
<Callout emoji="⚠️"> | ||
Em versões antigas (< 1.1.0), SWR compara os argumentos **superficialmente** em cada renderização e aciona a revalidação se algum deles foi alterado. | ||
</Callout> | ||
# Argumentos | ||
|
||
Por padrão, `key` será passado para `fetcher` como argumento. Então as 3 expressões a seguir são equivalentes: | ||
|
||
```js | ||
useSWR('/api/user', () => fetcher('/api/user')) | ||
useSWR('/api/user', url => fetcher(url)) | ||
useSWR('/api/user', fetcher) | ||
``` | ||
|
||
## Múltiplos Argumentos | ||
|
||
Em alguns cenários, é útil passar vários argumentos (pode ser qualquer valor ou objeto) para a função `fetcher`. | ||
Por exemplo, uma requisição de fetch autorizada: | ||
|
||
```js | ||
useSWR('/api/user', url => fetchWithToken(url, token)) | ||
``` | ||
|
||
Isso é **incorreto**. Porque o identificador (também a cache key) do dado é `'/api/user'`, | ||
mesmo se `token` mudar, SWR ainda usará a mesma chave e retornará o dado errado. | ||
|
||
Ao invés disso, você pode usar um array como o parâmetro `key`, que contém vários argumentos para a função `fetcher`. | ||
|
||
```js | ||
const { data: user } = useSWR(['/api/user', token], ([url, token]) => fetchWithToken(url, token)) | ||
``` | ||
|
||
The `fetcher` function accepts the `key` parameter as is, and the cache key will also be associated with the entire `key` argument. In the above example, `url` and `token` are both tight to the cache key. | ||
|
||
<Callout emoji="⚠️"> | ||
In older versions (< 2), The `fetcher` function accepts the `key` parameter as arguments separately | ||
</Callout> | ||
|
||
## Passando Objetos | ||
|
||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
<Callout> | ||
Desde a versão 1.1.0, chaves de objeto serão serializadas por baixo dos panos automaticamente. | ||
</Callout> | ||
|
||
Digamos que você tenha outra função que busca dados com um escopo de usuário: `fetchWithUser(api, user)`. Você pode fazer o seguinte: | ||
|
||
```js | ||
const { data: user } = useSWR(['/api/user', token], fetchWithToken) | ||
|
||
// ...e então passá-lo como argumento para outro hook useSWR | ||
const { data: orders } = useSWR(user ? ['/api/orders', user] : null, fetchWithUser) | ||
``` | ||
|
||
Você pode passar diretamente um objeto como chave, e `fetcher` receberá esse objeto também: | ||
|
||
```js | ||
const { data: orders } = useSWR({ url: '/api/orders', args: user }, fetcher) | ||
``` | ||
|
||
<Callout emoji="⚠️"> | ||
Em versões antigas (< 1.1.0), SWR compara os argumentos **superficialmente** em cada renderização e aciona a revalidação se algum deles foi alterado. | ||
</Callout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.