Skip to content

fix(docs): Complete server function mutation example and fix broken links. #3992

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

Merged
merged 7 commits into from
Apr 17, 2025
Merged
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
50 changes: 42 additions & 8 deletions docs/start/framework/react/learn-the-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,57 @@ Here's a quick example of how you can use server functions to perform a mutation

```tsx
import { createServerFn } from '@tanstack/react-start'
import { z } from 'zod'
import { dbUpdateUser } from '...'

const UserSchema = z.object({
id: z.string(),
name: z.string(),
})
export type User = z.infer<typeof UserSchema>

const updateUser = createServerFn({ method: 'POST' })
export const updateUser = createServerFn({ method: 'POST' })
.validator(UserSchema)
.handler(async ({ data }) => {
return db
.update(users)
.set({ name: data.name })
.where(eq(users.id, data.id))
})
.handler(({ data }) => dbUpdateUser(data))

// Somewhere else in your application
await updateUser({ data: { id: '1', name: 'John' } })
import { useQueryClient } from '@tanstack/react-query'
import { useRouter } from '@tanstack/react-router'
import { useServerFunction } from '@tanstack/react-start'
import { updateUser, type User } from '...'

export function useUpdateUser() {
const router = useRouter()
const queryClient = useQueryClient()
const _updateUser = useServerFunction(updateUser)

return useCallback(
async (user: User) => {
const result = await _updateUser({ data: user })

router.invalidate()
queryClient.invalidateQueries({
queryKey: ['users', 'updateUser', user.id],
})

return result
},
[router, queryClient, _updateUser],
)
}

// Somewhere else in your application
import { useUpdateUser } from '...'

function MyComponent() {
const updateUser = useUpdateUser()
const onClick = useCallback(async () => {
await updateUser({ id: '1', name: 'John' })
console.log('Updated user')
}, [updateUser])

return <button onClick={onClick}>Click Me</button>
}
```

To learn more about mutations, check out the [mutations guide](/router/latest/docs/framework/react/guide/data-mutations).
Expand Down