百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

use-clipboard-copy

> 前端框架
开源

用于 React 的轻量级复制到剪贴板的钩子

385 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 React 的轻量级复制到剪贴板的钩子

use-clipboard-copy

Table of Contents

  • Motivation
  • Getting Started
  • Usage
    • Copying Text of Another Target Element
    • Copying Text Imperatively (Without a Target Element)
    • Displaying a Temporary Success State
    • Handling Success and Errors
  • Browser Support
  • API
    • useClipboard(options?: UseClipboardOptions): ClipboardAPI
    • UseClipboardOptions
      • copiedTimeout?: number
      • onSuccess?: () => void
      • onError?: () => void
      • selectOnCopy?: boolean
      • selectOnError?: boolean
    • ClipboardAPI
      • copy: (text?: string) => void
      • target: React.RefObject
      • isSupported: () => boolean
      • copied: boolean
  • Acknowledgements
  • License

Motivation

There are various copy-to-clipboard solutions for Javascript – really good ones, but getting them to work with React can feel a little odd... they don't feel very React-y.

use-clipboard-copy is a lightweight ( { const url = Utils.formatUrl({ query: { id } }); clipboard.copy(url); // programmatically copying a value }, [clipboard.copy, id] );

return ( Copy Link ); }

bash

### Displaying a Temporary Success State

Sometimes it can be helpful to notify the user that the text was successfully copied to the clipboard, usually by displaying a temporary "Copied" state after they trigger the copy action.

By passing the `copiedTimeout` option to `useClipboard()`, you can use `clipboard.copied` as a way to toggle the copied state in the UI.

```jsx
import { useClipboard } from 'use-clipboard-copy';

export default function PublicUrl({ url }) {
  const clipboard = useClipboard({
    copiedTimeout: 600, // timeout duration in milliseconds
  });
  return (
    

  );
}

Handling Success and Errors

Copy to clipboard in browsers can be tricky some times - there are various reasons that can contribute to preventing the copy action from working. Therefore, you should always handle such cases.

By passing an onSuccess and onError callbacks to the useClipboard options, you will be able to handle whether the copy action was performed successfully.

javascript
function CopyText() {
  const clipboard = useClipboard({
    onSuccess() {
      console.log('Text was copied successfully!')
    },
    onError() {
      console.log('Failed to copy text!')
    }
  });
  return (
    

  );
}

…

jsx
function ClipboardSupport() {
  const clipboard = useClipboard();
  return (
    

  );
}

API

useClipboard(options?: UseClipboardOptions): ClipboardAPI

use-clipboard-copy exposes a named export useClipboard which is the hook function itself. It takes an optional options object, and returns an object to control the clipboard.

javascript
import { useClipboard } from 'use-clipboard-copy';

function CopyText() {
  const clipboard = useClipboard();
  return (
    // ...
  );
}

…

jsx
;
;

Optionally, copy takes a string to perform the copy action imperatively (programmatically). When this is used, the target element (if set) will be ignored.

javascript
 clipboard.copy('a text to copy')} />;

target: React.RefObject

A React Ref object used on input and textarea elements that holds the text to be copied.

javascript
;

isSupported: () => boolean

A function that returns a boolean indicating whether the browser supports the clipboard APIs. Useful to deterministically update the UI if the browser does not support the clipboard functionality for whatever reason.

copied: boolean

A boolean indicating whether the copy action was just performed. Must be used with the copiedTimeout option. Useful to update the UI to display temporary success state.

Acknowledgements

This hook is powered by clipboard-copy, the lightweight copy to clipboard for the web.

License

MIT

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptclipboardclipboard-copyclipboard-jsclipboardjs

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架