expo-clipboard
Version: 55.0.6
Provides an interface for getting and setting Clipboard content on Android, iOS and Web.
Installation
npx expo install expo-clipboard
Usage
import * as Clipboard from 'expo-clipboard';
// Set string
await Clipboard.setStringAsync('Hello, World!');
// Get string
const text = await Clipboard.getStringAsync();
console.log('Clipboard:', text);
// Set image (iOS/Android)
await Clipboard.setImageAsync('data:image/png;base64,...');
// Check if clipboard has content
const hasString = await Clipboard.hasStringAsync();
const hasImage = await Clipboard.hasImageAsync();
API Reference
Methods
setStringAsync(text)
(text: string) => Promise<void>
Sets clipboard textawait Clipboard.setStringAsync('Copied text');
Gets clipboard textconst text = await Clipboard.getStringAsync();
console.log('Clipboard:', text);
Sets clipboard text synchronously (deprecated)
Gets clipboard text synchronously (deprecated)
setImageAsync(base64Image)
(base64Image: string) => Promise<void>
Sets clipboard image (iOS/Android)await Clipboard.setImageAsync('data:image/png;base64,iVBORw0...');
getImageAsync()
() => Promise<string | null>
Gets clipboard image as base64 (iOS/Android)const imageBase64 = await Clipboard.getImageAsync();
Checks if clipboard has stringconst hasText = await Clipboard.hasStringAsync();
Checks if clipboard has image (iOS/Android)const hasImage = await Clipboard.hasImageAsync();
Examples
Copy to Clipboard
import * as Clipboard from 'expo-clipboard';
import { Button } from 'react-native';
function CopyButton({ text }: { text: string }) {
async function copyToClipboard() {
await Clipboard.setStringAsync(text);
alert('Copied to clipboard!');
}
return <Button title="Copy" onPress={copyToClipboard} />;
}
Paste from Clipboard
import * as Clipboard from 'expo-clipboard';
import { useState } from 'react';
import { View, Button, Text } from 'react-native';
export default function PasteExample() {
const [pastedText, setPastedText] = useState('');
async function paste() {
const text = await Clipboard.getStringAsync();
setPastedText(text);
}
return (
<View>
<Button title="Paste" onPress={paste} />
<Text>{pastedText}</Text>
</View>
);
}
Copy Image
import * as Clipboard from 'expo-clipboard';
import * as FileSystem from 'expo-file-system';
async function copyImageToClipboard(imageUri: string) {
// Read image as base64
const base64 = await FileSystem.readAsStringAsync(imageUri, {
encoding: FileSystem.EncodingType.Base64,
});
// Set to clipboard
await Clipboard.setImageAsync(`data:image/png;base64,${base64}`);
alert('Image copied!');
}
| Platform | String | Image |
|---|
| iOS | ✅ | ✅ |
| Android | ✅ | ✅ |
| Web | ✅ | ❌ |
Resources