yarn add @react-native-clipboard/clipboard
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
View,
Text,
} from 'react-native';
import Clipboard from '@react-native-community/clipboard'
class AwesomeProject extends Component {
state = {
content: 'Content will appear here'
};
//異步函數 箭頭函數不需要綁定this了
_setClipboardContent = async () => {
// 將文字復制到系統的粘貼板上,在系統其他的地方可以粘貼
Clipboard.setString('Hello World');
// 取出所存的值, Clipboard.getString() 返回的是以一個promise對象,所以可以在then里面存到state,或者使用同步存到state中
try {
var content = await Clipboard.getString();
this.setState({content});
} catch (e) {
this.setState({content:e.message});
}
};
render() {
return (
<View>
<Text onPress={this._setClipboardContent}
style={{color: 'blue',marginTop:100}}>
Tap to put "Hello World" in the clipboard
</Text>
<Text style={{color: 'red', marginTop: 20}}>
{this.state.content}
</Text>
</View>
);
}
}
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);