RN-Clipboard:復制文字到系統粘貼板上

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);

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容