How to Copy Text to Clipboard in React Native?

04-Apr-2023

.

Admin

How to Copy Text to Clipboard in React Native?

Hi Guys,

Today, I will let you know example of how to copy text to clipboard in react-native. you'll learn react native copy to clipboard example. if you want to see example of how to creat copy to clipboard in react native then you are a right place. I’m going to show you about how to implement copy to clipboard in react native. follow bellow step for how to use copy to clipboard in react native.

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('Nicesnippets.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...

#React Native