Sanity App SDK
    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>
    }
    • Create a new document, relying on Typegen for the initial-value type.

      Type Parameters

      • TDocumentType extends string = string
      • TDataset extends string = string
      • TProjectId extends string = string

      Parameters

      Returns (
          initialValue?: Partial<
              Omit<
                  SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>,
                  IgnoredKey,
              >,
          >,
          overrides?: CreateDocumentOverrides,
      ) => Promise<DocumentHandle<TDocumentType, TDataset, TProjectId>>

      A function that creates the document. It accepts optional initial field values and an optional {documentId} override, and resolves to the DocumentHandle of the created document (carrying the generated or supplied id).

    • Create a new document with an explicit type TData.

      Type Parameters

      • TData extends Record<string, unknown>

      Parameters

      • options: DocumentTypeHandle

        A document-type handle including documentType and optionally projectId/dataset/perspective.

      Returns (
          initialValue?: Partial<Omit<TData, IgnoredKey>>,
          overrides?: CreateDocumentOverrides,
      ) => Promise<DocumentHandle<string, string, string>>

      A function that creates the document. It accepts optional initial field values (typed against TData) and an optional {documentId} override, and resolves to the DocumentHandle of the created document.