Docs-logo

Expo

Get Started
API Reference
Slash-shortcut-icon
Hamburger-icon

Clipboard

expo-clipboard provides an interface for getting and setting Clipboard content on Android, iOS, and Web.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-success-iconStatus-success-icon

Installation

Terminal
→ expo install expo-clipboard

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

import * as React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import * as Clipboard from 'expo-clipboard';

export default function App() {
  const [copiedText, setCopiedText] = React.useState('');

  const copyToClipboard = async () => {
    await Clipboard.setStringAsync('hello world');
    };

  const fetchCopiedText = async () => {
    const text = await Clipboard.getStringAsync();
    setCopiedText(text);
  };

  return (
    <View style={styles.container}>
      <Button title="Click here to copy to Clipboard" onPress={copyToClipboard} />
      <Button title="View copied text" onPress={fetchCopiedText} />
      <Text style={styles.copiedText}>{copiedText}</Text>
    </View>
  );
}

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

import * as Clipboard from 'expo-clipboard';
⚠️
On Web, this module uses the AsyncClipboard API, which might behave differently between browsers or not be fully supported. Especially on WebKit, there's an issue which makes this API unusable in asynchronous code. Click here for more details.

NameTypeDescription
optionsGetImageOptionsA GetImageOptions object to specify the desired format of the image.

Gets the image from the user's clipboard and returns it in the specified format. Please note that calling this method on web will prompt the user to grant your app permission to "see text and images copied to the clipboard."

Example

const img = await Clipboard.getImageAsync({ format: 'png' });
// ...
<Image source={{ uri: img?.data }} style={{ width: 200, height: 200 }} />
  • Undo-iconPromise<ClipboardImage | null>

If there was an image in the clipboard, the promise resolves to a ClipboardImage object containing the base64 string and metadata of the image. Otherwise, it resolves to null.

NameTypeDescription
options
(optional)
GetStringOptionsOptions for the clipboard content to be retrieved.
Default: {}

Gets the content of the user's clipboard. Please note that calling this method on web will prompt the user to grant your app permission to "see text and images copied to the clipboard."

A promise that resolves to the content of the clipboard.

Only for:
Apple-iconiOS

Gets the URL from the user's clipboard.

A promise that fulfills to the URL in the clipboard.

Returns whether the clipboard has an image content.

On web, this requires the user to grant your app permission to "see text and images copied to the clipboard".

A promise that fulfills to true if clipboard has image content, resolves to false otherwise.

Returns whether the clipboard has text content. Returns true for both plain text and rich text (e.g. HTML).

On web, this requires the user to grant your app permission to "see text and images copied to the clipboard".

A promise that fulfills to true if clipboard has text content, resolves to false otherwise.

Only for:
Apple-iconiOS

Returns whether the clipboard has a URL content.

A promise that fulfills to true if clipboard has URL content, resolves to false otherwise.

NameTypeDescription
base64ImagestringImage encoded as a base64 string, without MIME type.

Sets an image in the user's clipboard.

Example

const result = await ImagePicker.launchImageLibraryAsync({
  mediaTypes: ImagePicker.MediaTypeOptions.Images,
  base64: true,
});
await Clipboard.setImageAsync(result.base64);
Warning-icon
Deprecated. Use setStringAsync() instead.

NameTypeDescription
textstring-

Sets the content of the user's clipboard.

  • Undo-iconvoid

On web, this returns a boolean value indicating whether or not the string was saved to the user's clipboard. On iOS and Android, nothing is returned.

NameTypeDescription
textstringThe string to save to the clipboard.
options
(optional)
SetStringOptionsOptions for the clipboard content to be set.
Default: {}

Sets the content of the user's clipboard.

On web, this returns a promise that fulfills to a boolean value indicating whether or not the string was saved to the user's clipboard. On iOS and Android, the promise always resolves to true.

Only for:
Apple-iconiOS

NameTypeDescription
urlstringThe URL to save to the clipboard.

Sets a URL in the user's clipboard.

NameTypeDescription
listener(event: ClipboardEvent) => voidCallback to execute when listener is triggered. The callback is provided a single argument that is an object containing information about clipboard contents.

Adds a listener that will fire whenever the content of the user's clipboard changes. This method is a no-op on Web.

Example

Clipboard.addClipboardListener(({ contentTypes }: ClipboardEvent) => {
  if (contentTypes.includes(Clipboard.ContentType.PLAIN_TEXT)) {
    Clipboard.getStringAsync().then(content => {
      alert('Copy pasta! Here\'s the string that was copied: ' + content)
    });
  } else if (contentTypes.includes(Clipboard.ContentType.IMAGE)) {
    alert('Yay! Clipboard contains an image');
  }
});

NameTypeDescription
subscriptionSubscriptionThe subscription to remove (created by addClipboardListener).

Removes the listener added by addClipboardListener. This method is a no-op on Web.

Example

const subscription = addClipboardListener(() => {
  alert('Copy pasta!');
});
removeClipboardListener(subscription);
  • Undo-iconvoid

NameTypeDescription
datastringA Base64-encoded string of the image data. Its format is dependent on the format option.
Info-icon
NOTE: The string is already prepended with data:image/png;base64, or data:image/jpeg;base64, prefix.
You can use it directly as the source of an Image element.
Example
<Image
  source={{ uri: clipboardImage.data }}
  style={{ width: 200, height: 200 }}
/>
size{ height: number, width: number }Dimensions (width and height) of the image pasted from clipboard.

NameTypeDescription
format'png' | 'jpeg'The format of the clipboard image to be converted to.
jpegQuality
(optional)
numberSpecify the quality of the returned image, between 0 and 1. Defaults to 1 (highest quality). Applicable only when format is set to jpeg, ignored otherwise.
Default: 1

NameTypeDescription
preferredFormat
(optional)
StringFormatThe target format of the clipboard string to be converted to, if possible.
Default: StringFormat.PLAIN_TEXT

NameTypeDescription
inputFormat
(optional)
StringFormatThe input format of the provided string. Adjusting this option can help other applications interpret copied string properly.
Default: StringFormat.PLAIN_TEXT

NameTypeDescription
contentstring
Warning-icon
Deprecated. Returns empty string. Use getStringAsync() instead to retrieve clipboard content.
-
contentTypesContentType[]An array of content types that are available on the clipboard.

NameTypeDescription
remove() => voidA method to unsubscribe the listener.

Type used to define what type of data is stored in the clipboard.

ContentType.HTML = "html"
ContentType.IMAGE = "image"
ContentType.PLAIN_TEXT = "plain-text"
Only for:
Apple-iconiOS

ContentType.URL = "url"

Type used to determine string format stored in the clipboard.

StringFormat.HTML = "html"
StringFormat.PLAIN_TEXT = "plainText"