javascriptreact-nativebuttontextonclick

How to copy text to clipboard in react-native?


I would like to integrate a small text (my e-mail address) but I would like the user can to copy this text. I think of a button, when we click on it the e-mail address is copied and it can be pasted 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}}> 
             mail@mail.com
</Text></View>

I am a novice, any help would be greatly appreciated.


Solution

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

    Here's how you can use it:

    import Clipboard from '@react-native-clipboard/clipboard';
    
    <TouchableOpacity onPress={() => Clipboard.setString('mail@mail.com')}>
      <View>
        <Text style={{color: 'red', fontSize: 14 , fontFamily:'Arial', fontStyle: 'bold', textAlign: 'center', marginTop: 3, marginLeft: 25, marginBottom: 17}}> 
                    mail@mail.com
        </Text>
      </View>
    </TouchableOpacity>