useDocumentRevisionGetter

Returns a function that reads the current document revision without re-rendering the component, for imperative checks inside event handlers and async flows, such as skipping a save when nothing has changed.

const getDocumentRevision = useDocumentRevisionGetter()

const revision = getDocumentRevision()
  • The function returns the same value as useDocumentRevision at the moment of the call, or undefined while no document is loaded.
  • Prefer useDocumentRevision when the revision is rendered or drives an effect: the getter is a one-time read.

Example

import { useRef } from 'react'
import { useDocumentRevisionGetter, useDocumentSaving } from '@sdk-pdf/react'

function useSaveToServer() {
  const getDocumentRevision = useDocumentRevisionGetter()
  const { prepareDocument } = useDocumentSaving()
  // The revision of the last successful save. Empty until then, so the first call always
  // saves.
  const savedRevisionRef = useRef<string | undefined>(undefined)

  return async () => {
    const revision = getDocumentRevision()
    if (revision === savedRevisionRef.current) return

    const buffer = await prepareDocument({ returnOriginalIfUnchanged: false })
    const response = await fetch('/api/documents', {
      method: 'PUT',
      body: buffer as BodyInit,
    })
    // A failed upload keeps the document unsaved.
    if (!response.ok) return

    // Records the revision the save started from: edits made during the upload stay
    // unsaved.
    savedRevisionRef.current = revision
  }
}

On this page