react-copy-to-clipboard is a React component that simplifies the process of copying text to the user's clipboard. It acts as a wrapper around the core `copy-to-clipboard` library, offering a declarative API for use within React applications. The component intelligently attempts to copy text using `document.execCommand('copy')`, falls back to the IE-specific `clipboardData` interface, and, if all else fails, provides a prompt for manual copying. The current stable version is 5.1.1. Releases are generally driven by dependency updates, bug fixes, or new React version compatibility. Its key differentiators include its simple, single-child wrapper pattern and its robust, multi-strategy approach to clipboard access, ensuring broad browser compatibility and reliability.
npm install react-copy-to-clipboardVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic React functional component using `CopyToClipboard` to copy text from an input field, showing visual feedback upon successful copy.
Ensure your project's `react` peer dependency is version 15.3.0 or higher. For older React versions, consider using `react-copy-to-clipboard` v4.x.
Update your `onCopy` callback function signature to accept two arguments: `(text, result) => { ... }`. Check the `result` boolean for copy success or failure.Ensure `CopyToClipboard` directly wraps exactly one React element, e.g., `<CopyToClipboard text="Hi"><button>Copy</button></CopyToClipboard>`. Providing multiple children or no children will cause a runtime error.
Verify that your project's `react` version is compatible with the peer dependency requirements, especially when working in React 18+ environments to avoid peer dependency warnings.
Ensure the component directly wraps exactly one React element, such as a `<button>` or `<span>`. For example: `<CopyToClipboard text="..."> <button>Copy</button> </CopyToClipboard>`.
Upgrade your project's `react` and `react-dom` packages to version 15.3.0 or higher, or downgrade `react-copy-to-clipboard` to a compatible v4.x release.
Ensure the `onCopy` prop is a valid function. If using v4.3.0 or later, update the `onCopy` callback signature to `(text, result) => { ... }` to correctly receive both arguments.