Skip to content

Commit 61e3363

Browse files
committed
docs: follow good practices
See #207
1 parent c11338f commit 61e3363

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

docs/guide/mutations.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,19 @@ import { defineMutation, useMutation } from '@pinia/colada'
7979

8080
export const useCreateTodo = defineMutation(() => {
8181
const todoText = ref('')
82-
const { mutate: createTodo, ...mutation } = useMutation({
83-
mutation: () =>
82+
const { mutate, ...mutation } = useMutation({
83+
mutation: (text: string) =>
8484
fetch('/api/todos', {
8585
method: 'POST',
86-
body: JSON.stringify({ text: todoText.value }),
86+
body: JSON.stringify({ text }),
8787
}),
8888
})
8989

9090
return {
9191
...mutation,
92-
createTodo,
92+
// we can still pass the todoText to the mutation so it appears in plugins
93+
// and other places
94+
createTodo: () => mutate(todoText.value),
9395
// expose the todoText ref
9496
todoText,
9597
}
@@ -115,6 +117,8 @@ const { createTodo, status, asyncStatus, todoText } = useCreateTodo()
115117

116118
:::
117119

120+
In the scenario above, the `todoText` is created only once and shared across all components that use the `useCreateTodo` composable.
121+
118122
When you just want to organize your mutations, you can also pass an object of options to `defineMutation()`:
119123

120124
```ts twoslash

0 commit comments

Comments
 (0)