useCopyToClipboard

React hook for copying a value to the clipboard.

Installation

npm install @mvdlei/hooks

Example

Usage

import React from "react";
import { useCopyToClipboard } from "@mvdlei/hooks";
 
const MyComponent = () => {
  const [copiedText, copy] = useCopyToClipboard();
 
  const handleCopyClick = async () => {
    const textToCopy = "Hello, clipboard!";
    const success = await copy(textToCopy);
 
    if (success) {
      console.log("Text copied successfully!");
    } else {
      console.error("Failed to copy text to clipboard.");
    }
  };
 
  return (
    <div>
      <button onClick={handleCopyClick}>Copy to Clipboard</button>
      <p>Copied Text: {copiedText}</p>
    </div>
  );
};

Reference

useCopyToClipboard(): [CopiedValue, CopyFn];

Return Value

An array containing the copied value (CopiedValue) and the copy function (CopyFn).

Parameters

None.

use-copy-to-clipboard.ts