Const AlphaA stable callback that triggers the action and resolves a Promise with the patch result.
This hook provides a stable callback to apply schema-validated patches to documents. Unlike useEditDocument, this uses the Agent Actions API which provides additional schema validation and safe merging capabilities.
Features:
documentId or targetDocument (mutually exclusive).schemaId (e.g., '_.schemas.default') and target to specify patch operations.async, noWrite, conditionalPaths.Each entry in target specifies a path, an operation ('set', 'append', 'mixed', or 'unset'),
and a value (required for all operations except 'unset').
import {useAgentPatch} from '@sanity/sdk-react'
function UpdateTitle({documentId}: {documentId: string}) {
const patch = useAgentPatch()
const handleUpdate = async () => {
const result = await patch({
documentId,
schemaId: '_.schemas.default',
target: [
{
path: 'title',
operation: 'set',
value: 'Updated Title',
},
{
path: 'lastModified',
operation: 'set',
value: new Date().toISOString(),
},
],
})
console.log('Patch result:', result)
}
return <button onClick={handleUpdate}>Update Title</button>
}
import {useAgentPatch} from '@sanity/sdk-react'
function AddTag({documentId}: {documentId: string}) {
const patch = useAgentPatch()
const handleAddTag = async (newTag: string) => {
await patch({
documentId,
schemaId: '_.schemas.default',
target: {
path: 'tags',
operation: 'append',
value: [newTag],
},
})
}
return (
<button onClick={() => handleAddTag('featured')}>
Add Featured Tag
</button>
)
}
import {useAgentPatch} from '@sanity/sdk-react'
function InsertContentBlock({
documentId,
afterKey,
}: {
documentId: string
afterKey: string
}) {
const patch = useAgentPatch()
const handleInsert = async () => {
await patch({
documentId,
schemaId: '_.schemas.default',
target: {
path: ['content', {_key: afterKey}],
operation: 'append',
value: [{_type: 'block', text: 'New paragraph inserted here.'}],
},
})
}
return <button onClick={handleInsert}>Insert Block</button>
}
import {useAgentPatch} from '@sanity/sdk-react'
function CreateProduct() {
const patch = useAgentPatch()
const handleCreate = async () => {
const result = await patch({
targetDocument: {
operation: 'create',
_type: 'product',
},
schemaId: '_.schemas.default',
target: [
{
path: 'title',
operation: 'set',
value: 'New Product',
},
{
path: 'price',
operation: 'set',
value: 29.99,
},
{
path: 'inStock',
operation: 'set',
value: true,
},
],
})
console.log('Created document:', result.documentId)
}
return <button onClick={handleCreate}>Create Product</button>
}
Schema-aware patching with Sanity Agent Actions.