Skip to content

Commit 664da75

Browse files
koba04huozhi
andauthored
docs: add a populateCache as a function (#329)
* docs: add a populateCache as a function * Update pages/docs/mutation.en-US.md Co-authored-by: Jiachi Liu <inbox@huozhi.im> * docs: add descriptions of arguments and a return value of the populateCache function Co-authored-by: Jiachi Liu <inbox@huozhi.im>
1 parent cd7a490 commit 664da75

File tree

7 files changed

+301
-147
lines changed

7 files changed

+301
-147
lines changed

pages/docs/mutation.en-US.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Profile () {
7474

7575
**`revalidate`**: should the cache revalidate once the asynchronous update resolves.
7676

77-
**`populateCache`**: should the result of the remote mutation be written to the cache.
77+
**`populateCache`**: should the result of the remote mutation be written to the cache, or a function that receives new result and current result as arguments and returns the mutation result.
7878

7979
**`rollbackOnError`**: should the cache rollback if the remote mutation errors.
8080

@@ -96,6 +96,28 @@ mutate('/api/todos', async todos => {
9696
// filter the list, and return it with the updated item
9797
const filteredTodos = todos.filter(todo => todo.id !== '1')
9898
return [...filteredTodos, updatedTodo]
99+
// Since the API already gives us the updated information,
100+
// we don't need to revalidate here.
101+
}, { revalidate: false })
102+
```
103+
104+
You can also use the `populateCache` option.
105+
106+
```jsx
107+
const updateTodo = () => fetch('/api/todos/1', {
108+
method: 'PATCH',
109+
body: JSON.stringify({ completed: true })
110+
})
111+
112+
mutate('/api/todos', updateTodo, {
113+
populateCache: (updatedTodo, todos) => {
114+
// filter the list, and return it with the updated item
115+
const filteredTodos = todos.filter(todo => todo.id !== '1')
116+
return [...filteredTodos, updatedTodo]
117+
},
118+
// Since the API already gives us the updated information,
119+
// we don't need to revalidate here.
120+
revalidate: false
99121
})
100122
```
101123

pages/docs/mutation.es-ES.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function Profile () {
7272

7373
**`revalidate`**: should the cache revalidate once the asynchronous update resolves.
7474

75-
**`populateCache`**: should the result of the remote mutation be written to the cache.
75+
**`populateCache`**: should the result of the remote mutation be written to the cache, or a function that receives new result and current result as arguments and returns the mutation result.
7676

7777
**`rollbackOnError`**: should the cache rollback if the remote mutation errors.
7878

@@ -94,6 +94,28 @@ mutate('/api/todos', async todos => {
9494
// filtrar la lista y devolverla con el item actualizado
9595
const filteredTodos = todos.filter(todo => todo.id !== '1')
9696
return [...filteredTodos, updatedTodo]
97+
// Since the API already gives us the updated information,
98+
// we don't need to revalidate here.
99+
}, { revalidate: false })
100+
```
101+
102+
You can also use the `populateCache` option.
103+
104+
```jsx
105+
const updateTodo = () => fetch('/api/todos/1', {
106+
method: 'PATCH',
107+
body: JSON.stringify({ completed: true })
108+
})
109+
110+
mutate('/api/todos', updateTodo, {
111+
populateCache: (updatedTodo, todos) => {
112+
// filter the list, and return it with the updated item
113+
const filteredTodos = todos.filter(todo => todo.id !== '1')
114+
return [...filteredTodos, updatedTodo]
115+
},
116+
// Since the API already gives us the updated information,
117+
// we don't need to revalidate here.
118+
revalidate: false
97119
})
98120
```
99121

pages/docs/mutation.ja.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Profile () {
7474

7575
**`revalidate`**: 非同期更新が解決した後、キャッシュを再検証するかどうか。
7676

77-
**`populateCache`**: リモートミューテーションの結果をキャッシュに書き込むかどうか。
77+
**`populateCache`**: リモートミューテーションの結果をキャッシュに書き込むかどうか。関数も受け取ります。関数はミューテーションの結果と現在の値を引数として受けとり、ミューテーションの結果を返します。
7878

7979
**`rollbackOnError`**: リモートミューテーションでエラーが発生した場合、キャッシュをロールバックするかどうか。
8080

@@ -96,6 +96,28 @@ mutate('/api/todos', async todos => {
9696
// リストをフィルタリングし、更新されたアイテムを返します
9797
const filteredTodos = todos.filter(todo => todo.id !== '1')
9898
return [...filteredTodos, updatedTodo]
99+
// API からすでに更新後の情報が取得できるため
100+
// 再検証する必要はありません
101+
}, { revalidate: false })
102+
```
103+
104+
`populateCache` オプションも使用可能です。
105+
106+
```jsx
107+
const updateTodo = () => fetch('/api/todos/1', {
108+
method: 'PATCH',
109+
body: JSON.stringify({ completed: true })
110+
})
111+
112+
mutate('/api/todos', updateTodo, {
113+
populateCache: (updatedTodo, todos) => {
114+
// リストをフィルタリングし、更新されたアイテムを返します
115+
const filteredTodos = todos.filter(todo => todo.id !== '1')
116+
return [...filteredTodos, updatedTodo]
117+
},
118+
// API からすでに更新後の情報が取得できるため
119+
// 再検証する必要はありません
120+
revalidate: false
99121
})
100122
```
101123

pages/docs/mutation.ko.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Profile () {
7474

7575
**`revalidate`**: 비동기 업데이트가 해소되면 캐시를 갱신합니다.
7676

77-
**`populateCache`**: 캐시에 쓰여질 원격 뮤테이션의 결과입니다.
77+
**`populateCache`**: should the result of the remote mutation be written to the cache, or a function that receives new result and current result as arguments and returns the mutation result.
7878

7979
**`rollbackOnError`**: 원격 뮤테이션 에러 시 캐시를 롤백합니다.
8080

@@ -96,6 +96,28 @@ mutate('/api/todos', async todos => {
9696
// 리스트를 필터링하고 업데이트된 항목을 반환합니다
9797
const filteredTodos = todos.filter(todo => todo.id !== '1')
9898
return [...filteredTodos, updatedTodo]
99+
// Since the API already gives us the updated information,
100+
// we don't need to revalidate here.
101+
}, { revalidate: false })
102+
```
103+
104+
You can also use the `populateCache` option.
105+
106+
```jsx
107+
const updateTodo = () => fetch('/api/todos/1', {
108+
method: 'PATCH',
109+
body: JSON.stringify({ completed: true })
110+
})
111+
112+
mutate('/api/todos', updateTodo, {
113+
populateCache: (updatedTodo, todos) => {
114+
// filter the list, and return it with the updated item
115+
const filteredTodos = todos.filter(todo => todo.id !== '1')
116+
return [...filteredTodos, updatedTodo]
117+
},
118+
// Since the API already gives us the updated information,
119+
// we don't need to revalidate here.
120+
revalidate: false
99121
})
100122
```
101123

0 commit comments

Comments
 (0)