Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy text to clipboard in react-native?

Tags:

I would like to integrate a small text (my e-mail address) but I would like the user can to copy this text. I thought of a button, when we click on it the e-mail address is copy and can be past outside the app. How to do this?

<View>
<Text style={{color: 'red', fontSize: 14 , fontFamily:'Arial', fontStyle: 'bold', textAlign: 'center', marginTop: 3, marginLeft: 25, marginBottom: 17}}> 
             [email protected]
</Text></View>

I am novice, any help would be greatly appreciated.

like image 730
Leeli Avatar asked Jul 14 '19 14:07

Leeli


People also ask

How do I copy text in react native?

You can use Clipboard from @react-native-community.

How do I copy text to clipboard?

Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

How do I copy to clipboard in react?

Now when we click on the button 'Copy to Clipboard', the function copyToClipboard gets triggered through onClick event which copies the state value to the clipboard with copy() function. Now we can copy our text anywhere by just clicking Ctrl+V key.


2 Answers

You can use Clipboard from @react-native-community.

Here's how you can use it:

import Clipboard from '@react-native-community/clipboard';

<TouchableOpacity onPress={() => Clipboard.setString('[email protected]')}>
  <View>
    <Text style={{color: 'red', fontSize: 14 , fontFamily:'Arial', fontStyle: 'bold', textAlign: 'center', marginTop: 3, marginLeft: 25, marginBottom: 17}}> 
                [email protected]
    </Text>
  </View>
</TouchableOpacity>
like image 167
fayeed Avatar answered Sep 21 '22 13:09

fayeed


Set selectable prop of the Text component true and the content will be copied to the clipboard with long press on the text.

<Text selectable={true}>
  Your text
</Text>
like image 27
ArmX Avatar answered Sep 25 '22 13:09

ArmX