React Native Copy to Clipboard

React Native Clipboard

Reading and writing from the Clipboard is incredibly easy in React Native by using the Clipboard API. It will work on both Android and IOs platform. If you are working with the app which provides codes or referral numbers then you can provide Copy to Clipboard feature.

To Import Clipboard Component in Code

import { Clipboard} from 'react-native'

Use of React Native Clipboard

Copy the value using

const clipboardContent = await Clipboard.getString();

Paste the value using

await Clipboard.setString(this.state.text);

In this example, We will copy the text inserted into TextInput and will show the copied text in Alert on a click of a button. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Code

Open App.js in any code editor and replace the code with the following code

App.js

/*This is an Example of React Native Copy to Clipboard*/
import React from 'react';
//import React in our app
import { StyleSheet, Text, View, Button, TextInput, Clipboard } from 'react-native';
//import all the needed React Native components 

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      clipboardContent: null,
    };
  }
  readFromClipboard = async () => {
    //To get the text from clipboard
    const clipboardContent = await Clipboard.getString();   
    this.setState({ clipboardContent }); 
  };
  writeToClipboard = async () => {
    //To copy the text to clipboard
    await Clipboard.setString(this.state.text);
    alert('Copied to Clipboard!');
  };
  render() {
    return (
      <View style={styles.container}>
        <TextInput
          style={styles.textInput}
          onChangeText={(text) => this.setState({text})}
          value={this.state.text}
          placeholder="Type here..."/>
        <Button
          onPress={this.writeToClipboard}
          title="Copy to Clipboard"/>
        <Text style={styles.boldText}>Clipboard Contents: </Text>
        <Text>{this.state.clipboardContent}</Text>
        <Button
          onPress={this.readFromClipboard}
          title="Paste from Clipboard"
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    marginTop:50,
  },
  boldText: {
    fontWeight: '600',
    marginTop:60,
  },
  textInput: {
    height: 40,
    borderColor: 'gray',
    borderWidth: 1,
    width: '80%',
    paddingHorizontal: 10,
    marginBottom:20,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

Android

      

IOS

Output in Online Emulator

That was the way to copy some text to Clipboard in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. The remaining components will be covered in the next article. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.