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 platforms. If you are working with an app that provides codes or referral numbers then you can provide Copy to Clipboard feature.
Clipboard API is now split out from the core of React Native. Now you can use Clipboard as a Clipboard API or as a useClipboard hook.
Clipboard API
To use Clipboard API you need to import the API using
import Clipboard from '@react-native-community/clipboard'
Use of React Native Clipboard API
Copy to Clipboard
const clipboardContent = Clipboard.getString();
Get the value from Clipboard
await Clipboard.setString(this.state.text);
Clipboard Hook
To use Clipboard Hook first import Hook
import { useClipboard } from '@react-native-community/clipboard'
Make the hook getter setter
const [data, setString] = useClipboard();
Use of React Native Clipboard Hook
Copy to Clipboard
setString('hello world');
Get the value from Clipboard
{data}
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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use Clipboard API or Clipboard Hook we need to install @react-native-community/clipboard
dependency.
To install this open the terminal and jump into your project using
cd ProjectName
Run the following command to install
npm install --save @react-native-community/clipboard
This command will copy all the dependencies into your node_module directory.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
Code
Open App.js in any code editor and replace the code with the following code
App.js
// React Native Copy to Clipboard
// https://aboutreact.com/react-native-copy-to-clipboard/
// import React in our code
import React, {useState} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
View,
Text,
TouchableOpacity,
TextInput,
} from 'react-native';
// Using Clipboard API
import Clipboard from '@react-native-community/clipboard';
// Using Clipboard Hook
import {useClipboard} from '@react-native-community/clipboard';
const App = () => {
const [input1, setInput1] = useState('');
const [input2, setInput2] = useState('');
// For Clipboard Hook
const [data, setString] = useClipboard();
const readFromClipboard = async () => {
//To get the text from clipboard
let clipboardContent = await Clipboard.getString();
alert('Text from Clipboard: ' + clipboardContent);
};
const writeToClipboard = async () => {
//To copy the text to clipboard
Clipboard.setString(input1);
alert('Copied to Clipboard!');
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.container}>
<Text style={styles.titleText}>
React Native Copy to Clipboard
</Text>
<Text style={styles.titleTextsmall}>
Using Clipboard API
</Text>
<TextInput
style={styles.textInput}
onChangeText={(input1) => setInput1(input1)}
value={input1}
placeholder="Type here..."
/>
<View style={{flexDirection: 'row'}}>
<TouchableOpacity
activeOpacity={0.7}
style={styles.buttonStyle}
onPress={writeToClipboard}>
<Text style={styles.buttonTextStyle}>
Copy to Clipboard
</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={0.7}
style={styles.buttonStyle}
onPress={readFromClipboard}>
<Text style={styles.buttonTextStyle}>
Get from Clipboard
</Text>
</TouchableOpacity>
</View>
<View style={styles.lineStyle} />
<Text style={styles.titleTextsmall}>
Using Clipboard Hook
</Text>
<TextInput
style={styles.textInput}
onChangeText={(input2) => setInput2(input2)}
value={input2}
placeholder="Type here..."
/>
<View style={{flexDirection: 'row'}}>
<TouchableOpacity
activeOpacity={0.7}
style={styles.buttonStyle}
onPress={() => {
setString(input2);
alert('Copied to Clipboard!');
}}>
<Text style={styles.buttonTextStyle}>
Copy to Clipboard
</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={0.7}
style={styles.buttonStyle}
onPress={
() => alert('Text from Clipboard: ' + data)
}>
<Text style={styles.buttonTextStyle}>
Get from Clipboard
</Text>
</TouchableOpacity>
</View>
</View>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
padding: 10,
textAlign: 'center',
},
titleText: {
fontSize: 22,
textAlign: 'center',
fontWeight: 'bold',
},
titleTextsmall: {
textAlign: 'center',
marginVertical: 8,
fontSize: 16,
},
buttonStyle: {
justifyContent: 'center',
marginTop: 15,
width: '50%',
padding: 10,
backgroundColor: '#8ad24e',
marginRight: 2,
marginLeft: 2,
},
buttonTextStyle: {
color: '#fff',
textAlign: 'center',
},
textInput: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
width: '100%',
paddingHorizontal: 10,
},
lineStyle: {
color: 'black',
borderWidth: 2,
margin: 10,
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
That was the way to copy some text to Clipboard in React Native. If you have any doubts or 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. 🙂