How to implement the copy function in react: 1. Implement the copy function through the "copy-to-clipboard" library; 2. Use the "react-copy-to-clipboard" library to implement the copy function; 3. Through the "navigator" .clipboard.writeText(e)" method to realize copying; 4. To realize copying through "document.execcommand("copy")" method; 5. To realize copy function through "copy-js" library.
#How to implement the copy function in react?
One-click copying in React - five methods
// npm安装---这种方式可能会对babel的版本有限制 npm i --save copy-to-clipboard //cdn引入 <script src="https://wzrd.in/standalone/copy-to-clipboard@latest" async></script>
import copy from 'copy-to-clipboard';const handleClick = ()=>{ copy('复制的内容'); message.success('复制成功')}<Button onClick={handleClick}>复制</Button>
1. Installation
npm i --save react-copy-to-clipboard
to pay attention to, in , there can only be one root element, and I tried it myself, if in
, a root element is wrapped If there are two sibling nodes such as a div and a button, the copy will not take effect. I don't know why. Interested friends can check out the source code.
import { CopyToClipboard } from 'react-copy-to-clipboard'; <CopyToClipboard text={'复制的内容'} onCopy={(_, result) => { if (result) { message.success('复制成功'); } else { message.error('复制失败,请稍后再试'); } }} > <Button type='primary' icon={<CopyOutlined />} /> </CopyToClipboard>
for details I I have never used this method, and I don’t know what the pitfalls are.
<button id="btn" style="margin-top: 40px;">一键复制</button>const btn = document.querySelector('#btn'); btn.addEventListener('click', () => { const textarea= document.createElement('textarea'); textarea.setAttribute('readonly', 'readonly'); textarea.value = 'xxxxx'; document.body.appendChild(textarea); textarea.select(); if (document.execCommand('copy')) { document.execCommand('copy'); alert('复制成功'); } document.body.removeChild(textarea); })
document.execcommand("copy") 1. Install
// npm包下载npm install copy-js --save// CDN导入<script src="//cdn.rawgit.com/duyetdev/copy.js/master/dist/copy.min.js"></script>
import copy from 'copy-to-clipboard';copy('hello world', function(err) { if (err) console.log('Some thing went wrong!'); console.log('Copied!');});
The parameter e of this method needs to get the value of the input text box and copy it. Node
However, this method may have limitations in the in-end browser of some applications. It can be used in normal browsers, but for example, it is now available in the Feishu end-end browser. There is no clipboard object. It still depends on the usage scenario.1. How to use
const { Search } = Input;const copyLink = (e: any) => { navigator.clipboard.writeText(e).then( () => { message.success(intl.t('复制成功')); console.log(e); }, () => { message.error(intl.t('复制失败,请稍后再试')); }, );}; <Search bordered={false} value={window.location.href} enterButton={intl.t('复制链接')} size='middle' onSearch={copyLink} />
react video tutorial 》
The above is the detailed content of How to implement copy function in react. For more information, please follow other related articles on the PHP Chinese website!