Trying to use React Native Community Clipboard. When using the setClipboard function like so:
import Clipboard from '@react-native-community/clipboard';
/*
More code here
*/
Clipboard.setString("Hello");
I receive an error:
TypeError: Cannot read property 'setString' of null
However, when logging Clipboard or even Clipboard.setString, I can see that it isn't null:
console.log(Clipboard.setString); // Outputs the log: [Function setString]
Even if I check the function exists first, I see the same error:
Clipboard.setString && Clipboard.setString("Hello"); // TypeError: Cannot read property 'setString' of null
How is it possible that the Clipboard object can be null only when I try to invoke a function? Is it possible that there's some overlap with something else in the repo I'm working in?
There's some discussion here that has a few solutions/workarounds.
A couple examples they mention are
use the hook instead:
import {useClipboard} from '@react-native-community/clipboard';
or possibly using Clipboard from react-native:
import { Clipboard } from 'react-native';
Hope one of these helps!