Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: Error setting and getting text from ClipBoard

Tags:

react-native

import React, { useState } from 'react'
import { SafeAreaView, View, Text, TouchableOpacity, StyleSheet } from 'react-native'
import Clipboard from '@react-native-community/clipboard'

const App = () => {
    const [copiedText, setCopiedText] = useState('')

    const copyToClipboard = () => {
        Clipboard.setString('hello world')
    }

    const fetchCopiedText = async () => {
        const text = await Clipboard.getString()
        setCopiedText(text)
    }

    return (
        <SafeAreaView style={{ flex: 1 }}>
            <View style={styles.container}>
                <TouchableOpacity onPress={() => copyToClipboard()}>
                    <Text>Click here to copy to Clipboard</Text>
                </TouchableOpacity>
                <TouchableOpacity onPress={() => fetchCopiedText()}>
                    <Text>View copied text</Text>
                </TouchableOpacity>

                <Text style={styles.copiedText}>{copiedText}</Text>
            </View>

        </SafeAreaView>
    )
}

const styles = StyleSheet.create({
//styles
})

export default App

When pressing "copy to clipboard" i get an error saying null is not and object('evaluating NativeClipboard_1.default.setString') and on pressing "view copied text" i get an TypeError Unhandlded promise rejection. This code was copied directly from here: https://github.com/react-native-community/clipboard

enter image description here

like image 928
parv desai Avatar asked Mar 31 '20 07:03

parv desai


1 Answers

According to the Expo documentation (https://docs.expo.io/versions/v40.0.0/sdk/clipboard/), there's a Clipboard available from their API.

Install with

expo install expo-clipboard

and use with

import Clipboard from 'expo-clipboard';
like image 74
Irv Katz Avatar answered Nov 09 '22 23:11

Irv Katz