How to use Copy to Clipboard in React Native?

Published On: 21/06/2022 | Category: React Native

Hi Guys,

I will explain step by step tutorial how to use copy to clipboard in react native. I explained simply about copy to clipboard example in react native. you can understand a concept of how to creat copy to clipboard in react native. you will learn how to implement copy to clipboard in react native. So, let's follow few step to create example of react native copy to clipboard example.

Let's start following example:

Step 1: Download Project

In the first step run the following command to create a project.

expo init ExampleApp
Step 2: Install and Setup

First, you need to install them in your project:

expo install expo-clipboard
Step 3: App.js

In this step, You will open the App.js file and put the code.

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

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

    const copyToClipboard = async () => {
        await Clipboard.setStringAsync('Tuts-Station.com');
    };

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

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

const styles = StyleSheet.create({
    container: {
        paddingTop: 50,
        paddingHorizontal: 20,
    },
    buttonContainer: {
        marginTop:10,
    },
});

export default App;
Run Project

In the last step run your project using the below command.

expo start

You can QR code scan in Expo Go Application on mobile.

Output :

It will help you...