8
votes
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

8
Hello, which RN version are you using?FreakyCoder
It is version 0.61.4parv desai
I would normally point this to a linking error, but I'm getting the same thing after linking the library.Cory McAboy
I figured it out. See the answer below.Cory McAboy

8 Answers

8
votes

I too had this issue, as other users have said, Clipboard from react-native-community is not compatible with Expo.

6
votes

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';
5
votes

Use react clipboard

Running example: https://snack.expo.io/@msbot01/4c673f

code:

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

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 style={{marginTop:50}} onPress={() => fetchCopiedText()}>
          <Text>Click to View copied text</Text>
        </TouchableOpacity>

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

    </SafeAreaView>
  ) 
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  copiedText: {
    marginTop: 10,
    color: 'red'
  }
})

export default App
4
votes

I had the same issue. It ended up being a linking issue. I ran react-native link like the instructions asked, but I forgot to install the pod. Make sure you install pods after linking.

cd ios && pod install && cd ..
1
votes

manual linking worked for me:

mobile/android/settings.gradle

include ':@react-native-community-clipboard'
project(':@react-native-community-clipboard').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/clipboard/android')

mobile/android/app/build.gradle

dependencies {
  ...
  implementation project(':@react-native-community-clipboard')
  ...
}

MainApplication.java

import com.reactnativecommunity.clipboard.ClipboardPackage;

...

@Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          ....
          packages.add(new ClipboardPackage());
          
          return packages;
        }
0
votes

I had the same issue, after much research could find a solution by using

import { SafeAreaView, View, Text, TouchableOpacity, Clipboard, StyleSheet } from 'react-native'

instead of separately using

import Clipboard from '@react-native-community/clipboard'

In expo @react-native-community/x packages are not used.

0
votes

Aside from expo compatibility issues, This error appears if you forget to rebuild your app again after installing Clipboard Package from rn-community repo; Did you try to rebuild your app again, After linking the module?

-1
votes

Dude, not use the clipboard from 'react-native', use from '@react-native-community/clipboard', as it will be removed. Follow steps:

1 - yarn add @react-native-community/clipboard

2 - react-native run-android

3 - yarn start

4 - success...