React项目中复制(react-copy-to-clipboard)

react复制内容到剪贴板 ---- 依赖包react-copy-to-clipboard

一、安装插件依赖包(react-copy-to-clipboard)

npm install --save copy-to-clipboard

二、引用插件

import copy from 'copy-to-clipboard';

三、实际项目应用

import React, { Component } from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Button, message } from 'antd';
export default class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
    	value: '要复制内容',
    }
  }

  render() {
    return (
  		<>
	  		<CopyToClipboard text={this.state.value}
	          onCopy={() => message.success('复制成功~')}>
	          <Button type='primary'>复制</Button>
	        </CopyToClipboard>
  		</>
    );
  }
}

具体解释参考网址:
https://www.npmjs.com/package/react-copy-to-clipboard

你可能感兴趣的:(react,react,javascript,reactjs)