Course contentsShow
← React Native
Lesson 177 of 1,5553. Core Primitive ComponentsFree lesson

Handling Clipboard and Text Copy

Learners will leverage system-level interactions to allow users to copy and paste text content.

In this lesson, you will learn how to interact with the device’s system clipboard to allow users to copy text content directly from your application.

The System Clipboard

Mobile operating systems provide a shared, temporary storage space called the "clipboard." When a user "copies" text, it is sent to this system-level buffer; when they "paste," the device retrieves it. In React Native, we can programmatically bridge our app to this system feature using the Clipboard module.

Implementing a Copy Interaction

To let a user copy text, we typically trigger an action from a UI element, such as a button or a long-press on a text block. Since we have already mastered State-Driven Button Interactions, we can easily wire a copyToClipboard function to an onPress event.

Here is how you can push a string into the user's system clipboard:

import { Clipboard, Button, View, Text } from 'react-native';

const CopyExample = () => {
  const textToCopy = "Hello, React Native!";

  const handleCopy = () => {
    Clipboard.setString(textToCopy);
    alert('Text copied to clipboard!');
  };

  return (
    <View>
      <Text>{textToCopy}</Text>
      <Button title="Copy Text" onPress={handleCopy} />
    </View>
  );
};

Best Practices for User Experience

When implementing copy functionality, keep these points in mind:

  1. Visual Feedback: Because the clipboard operation happens in the background, the user won't know it succeeded unless you tell them. Using an alert, a toast message, or a temporary change in the button’s label (e.g., changing "Copy" to "Copied!") is essential for a polished experience.
  2. User Intent: Users expect specific behaviors. Copying is most intuitive when triggered by a clearly labeled button or a long-press gesture on an input field or static text block.
  3. Permissions: Unlike camera or location access, interacting with the clipboard is generally permitted by the OS without explicit user prompts, making it a low-friction way to improve productivity within your app.

By integrating the clipboard, you turn static information into dynamic data that the user can move to other applications like Notes, Email, or Messaging apps.

Key Takeaway: The Clipboard module allows you to programmatically bridge your app to the device’s system-level buffer, enabling seamless text portability for your users.