Skip to main content

Overview

The init() function is the primary entry point for React Grab. It initializes the library, sets up event listeners, and returns an API instance that you can use to control React Grab programmatically.

Signature

Parameters

Options
Configuration options for React Grab

Return Value

ReactGrabAPI
Returns an API instance with methods to control React Grab programmatically. See ReactGrabAPI for details.

Usage

Basic Initialization

With Custom Options

With Custom Activation Key

With Custom Content Generator

Disabling React Grab

Behavior

  • Single Initialization: The function can only be called once successfully. Subsequent calls return a no-op API.
  • Server-Side Rendering: When called in a non-browser environment (e.g., during SSR), returns a no-op API.
  • Built-in Plugins: Automatically registers built-in plugins (copy, comment, copyHtml, copyStyles, open).
  • Event Listeners: Sets up keyboard, mouse, and touch event listeners for element selection.
  • Cleanup: The returned API includes a dispose() method to clean up all listeners and resources.

Notes

  • The enabled option cannot be changed via setOptions() after initialization. Use setEnabled() on the API instead.
  • Options can be provided via a <script> tag with data-react-grab-options attribute, which will be merged with the options passed to init().
  • The activation key defaults to Cmd+C on Mac and Ctrl+C on Windows/Linux.