Sanity Library Reference Docs
    Preparing search index...

    Provides a function to create a new document and returns its handle.

    This is the create counterpart to useEditDocument. It wraps useApplyDocumentActions and the createDocument action for the common single-document case, so you don't have to assemble the action by hand.

    It handles the document ID for you: if you don't supply one (on the handle or via the per-call {documentId} override), a UUID is generated. Either way the returned DocumentHandle carries that id, ready to pass to useDocument, useEditDocument, or your router.

    Unlike useEditDocument, this hook does not read existing document state, so it never suspends.

    For atomic create-and-publish, or for creating several documents in a single transaction, use useApplyDocumentActions with the createDocument and publishDocument action creators directly.

    import {useCreateDocument} from '@sanity/sdk-react'
    import {useNavigate} from 'react-router-dom'

    function CreateArticleButton() {
    const createArticle = useCreateDocument({documentType: 'article'})
    const navigate = useNavigate()

    const handleClick = async () => {
    const handle = await createArticle({title: 'New Article'})
    navigate(`/articles/${handle.documentId}`)
    }

    return <button onClick={handleClick}>Create Article</button>
    }