usePageIndexGetter

Returns a getter that resolves a page id to its index in the document, without subscribing the component to page order changes.

const getPageIndex = usePageIndexGetter()

const pageIndex = getPageIndex(pageId) // number | undefined
  • The getter returns undefined when the page is deleted or unknown: an id can go stale, so handle that case.
  • Use it where the index is needed occasionally, such as inside an event handler. When the resolved index is rendered, pair it with useRenderOnPageOrderChange so the component re-renders when the order changes; for a rendered, always-fresh list, prefer usePages.

Example

import { usePageIndexGetter } from '@sdk-pdf/react'

function useCopyPageLink() {
  const getPageIndex = usePageIndexGetter()

  return (pageId: string) => {
    const pageIndex = getPageIndex(pageId)
    if (pageIndex === undefined) return

    void navigator.clipboard.writeText(
      `${location.origin}${location.pathname}#page=${pageIndex + 1}`
    )
  }
}

On this page