# useSelectedBlocksGetter

Source: https://sdk.pdf.net/docs/v1/reference/useSelectedBlocksGetter



Returns a function that reads the currently selected blocks as snapshots, without subscribing the component to changes. Use it in event handlers, such as a toolbar button or a hotkey; to render the selection, use [useSelectedBlocks](https://sdk.pdf.net/docs/v1/reference/useSelectedBlocks).

```ts
const getSelectedBlocks = useSelectedBlocksGetter()

const selectedBlocks = getSelectedBlocks() // Block[]
```

* The entries are the same `Block` snapshots as in [useSelectedBlocks](https://sdk.pdf.net/docs/v1/reference/useSelectedBlocks), read at the moment of the call.
* With no document loaded, the function returns an empty array.

## Example [#example]

```tsx
import { useSelectedBlocksGetter, useSetWidgetProps } from '@sdk-pdf/react'

function LockSelectedFieldsButton() {
  const getSelectedBlocks = useSelectedBlocksGetter()
  const setWidgetProps = useSetWidgetProps()

  const handleClick = () => {
    const widgetIds = getSelectedBlocks()
      .filter((block) => block.type === 'annotation' && block.subtype === 'Widget')
      .map((block) => block.id)

    void setWidgetProps(widgetIds, { isReadOnly: true })
  }

  return <button onClick={handleClick}>Lock selected fields</button>
}
```

