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
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With