1. react-copy-to-clipboard
链接: https://github.com/nkbt/react-copy-to-clipboard
安装
npm install --save react-copy-to-clipboard
使用方法
text 属性 为要复制的内容
import React from 'react';
import ReactDOM from 'react-dom';
import {CopyToClipboard} from 'react-copy-to-clipboard';
class App extends React.Component {
state = {
value: '',
copied: false,
};
render() {
return (
<div>
<input value={this.state.value}
onChange={({target: {value}}) => this.setState({value, copied: false})} />
<CopyToClipboard text={this.state.value}
onCopy={() => this.setState({copied: true})}>
<span>Copy to clipboard with span</span>
</CopyToClipboard>
<CopyToClipboard text={this.state.value}
onCopy={() => this.setState({copied: true})}>
<button>Copy to clipboard with button</button>
</CopyToClipboard>
{this.state.copied ? <span style={{color: 'red'}}>Copied.</span> : null}
</div>
);
}
}
const appRoot = document.createElement('div');
document.body.appendChild(appRoot);
ReactDOM.render(<App />, appRoot);
2、react-native自带的Clipboard
import React, { useState } from 'react'
import { View, Text, Clipboard } from 'react-native'
export class InvoiceInfo extends Component {
constructor(props){
super(props);
this.state = {
text: "复制的内容"
}
}
render() {
return (
<View>
<Text onPress={self._onPressCopy} >复制</Text>
</View>
);
}
_onPressCopy= () => {
Clipboard.setString(this.state.text);
Toast.show("已复制成功");
}