clipboard icon indicating copy to clipboard operation
clipboard copied to clipboard

Clipboard is null

Open narkai opened this issue 3 years ago • 12 comments

Environment

React Native with Expo 39

Platforms

Android

Versions

  • Android: 10
  • react-native: 63

Description

...

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

...

const Component = () => {

  useEffect(() => {
    const check = async () => {
      const clipboard = await Clipboard.getString();
      if (clipboard) {
        setClipboard(clipboard);
        Clipboard.setString('');
    };
    check();
  }, []);

...

This code throws this error: null is not an object (evaluating 'NativeClipboard_1.default.getString')

But strangely, when I test the code with Jest it works... Maybe Clipboard is not yet mounted properly when the effect is triggered.

narkai avatar Nov 04 '20 11:11 narkai

I had the same issue with Android. Weird enough I had to do a pod install: cd ios && pod install && cd ../ after that, I ran react-native run-android and it worked.

Jorciney avatar Nov 07 '20 19:11 Jorciney

I am with this issue too, but in the Native enviroment. Gonna try this too @Jorciney .

dheysonalves avatar Nov 10 '20 19:11 dheysonalves

@Dheyson good luck!

Jorciney avatar Nov 11 '20 11:11 Jorciney

@Jorciney are you using React Native with Expo (managed project) ?

Unfortunately I think it's not recommended to use pod install with Expo (managed project)...

narkai avatar Nov 11 '20 11:11 narkai

@lapico You are right, I'm not using Expo.

Jorciney avatar Nov 11 '20 11:11 Jorciney

Seems like using the native alternative (deprecated) is the only workaround for now. The community version just doesn't work with Expo. I tried a lot, including using useClipboard hook, but have to now settle with using the deprecated version. At least it doesn't throw that ugly error.

roshnet avatar Nov 22 '20 16:11 roshnet

I confirm this, I also use the deprecated version because the community one does not work with Expo.

Strange fact, the community version seemed to work in the tests (Jest).

narkai avatar Nov 23 '20 08:11 narkai

Unfortunately, this module is not compatible with Expo, since expo only works with preinstalled native modules. If you're using Expo with managed workflow, please use the Clipboard module exported from react-native

Naturalclar avatar Nov 29 '20 09:11 Naturalclar

I solved it this way: cd android && ./gradlew clean

iqorlobanov avatar Jan 13 '21 17:01 iqorlobanov

I solved it this way: cd android && ./gradlew clean

This answer is the correct one. thank you!

BiYuqi avatar Nov 03 '21 07:11 BiYuqi

Stil this is open?

ajstharsan-radus28 avatar Feb 25 '22 11:02 ajstharsan-radus28

I am still facing this issue. Any update on this?

kunalsolanki1992 avatar Dec 06 '22 13:12 kunalsolanki1992