Hello friends, In today’s tutorial we would learn about a copy paste to clipboard package in react native. The package name is react native community clipboard. The best thing of this package is that it can make copy and paste easy in react native application. We can easily store the copied text and paste it anywhere in our application. So in this tutorial we would learn about Example of React Native Community Clipboard NPM Package.
Contents in this project Example of React Native Community Clipboard NPM Package :-
1. First of all we have to install the react-native-clipboard/clipboard package in our react native project. I’m using NPM command to install the package. So open your project’s root directory in CMD or Terminal and execute below command.
1
|
npm install —save @react–native–clipboard/clipboard
|
Screenshot :-
2. Now open your project’s main App.js file and import useState, SafeAreaView, Text, TextInput, StyleSheet and Alert component.
1
2
3
|
import React, { useState } from ‘react’;
import { SafeAreaView, Text, TextInput, StyleSheet, Alert } from ‘react-native’;
|
3. Importing Clipboard from @react-native-community/clipboard package.
1
|
import Clipboard from ‘@react-native-community/clipboard’;
|
4. Creating our main App component.
1
2
3
4
|
export default function App() {
}
|
5. Creating a State named as text with State update method setText with default Text. We would use this state to show some text on the app screen.
1
|
const [text, setText] = useState(‘This is some sample Text to copy in React Native. Long Press on Text to COPY.’);
|
6. Creating another State named as newText with setNewText state update method. We would use this State to show HINT in Text Input component. We would also use this state to update Text Input value.
1
|
const [newText, setNewText] = useState(‘Paste Text Here….’);
|
7. Creating a function named as copyToClipboard(). In this function we would simply copy the text.
1
2
3
4
|
const copyToClipboard = () => {
Clipboard.setString(text);
Alert.alert(‘Text Copied Successfully…’);
};
|
8. Creating another function named as fetchCopiedText(). In this function we would access the copied text and set the text into State. This would make the text paste inside the text input component.
1
2
3
4
5
|
const fetchCopiedText = async () => {
const temp = await Clipboard.getString();
setNewText(temp);
console.log(temp);
};
|
9. Creating return() block. Now here we would make 1 Text and 1 TextInput component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
return (
<SafeAreaView style={styleSheet.MainContainer}>
<Text style={styleSheet.text}
onLongPress={copyToClipboard}>
{text}
</Text>
<TextInput
placeholder= {newText}
underlineColorAndroid=‘transparent’
style={styleSheet.textInput}
onLongPress={fetchCopiedText}
/>
</SafeAreaView>
);
|
10. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
alignItems: ‘center’,
backgroundColor: ‘white’
},
text: {
fontSize: 25,
textAlign: ‘center’,
color: ‘black’,
padding: 10
},
textInput: {
textAlign: ‘center’,
height: 50,
width: ‘90%’,
borderWidth: 1,
borderColor: ‘red’,
borderRadius: 7,
}
});
|
11. Complete Source Code for App.js file :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import React, { useState } from ‘react’;
import { SafeAreaView, Text, TextInput, StyleSheet, Alert } from ‘react-native’;
import Clipboard from ‘@react-native-community/clipboard’;
export default function App() {
const [text, setText] = useState(‘This is some sample Text to copy in React Native. Long Press on Text to COPY.’);
const [newText, setNewText] = useState(‘Paste Text Here….’);
const copyToClipboard = () => {
Clipboard.setString(text);
Alert.alert(‘Text Copied Successfully…’);
};
const fetchCopiedText = async () => {
const temp = await Clipboard.getString();
setNewText(temp);
console.log(temp);
};
return (
<SafeAreaView style={styleSheet.MainContainer}>
<Text style={styleSheet.text}
onLongPress={copyToClipboard}>
{text}
</Text>
<TextInput
placeholder= {newText}
underlineColorAndroid=‘transparent’
style={styleSheet.textInput}
onLongPress={fetchCopiedText}
/>
</SafeAreaView>
);
};
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
alignItems: ‘center’,
backgroundColor: ‘white’
},
text: {
fontSize: 25,
textAlign: ‘center’,
color: ‘black’,
padding: 10
},
textInput: {
textAlign: ‘center’,
height: 50,
width: ‘90%’,
borderWidth: 1,
borderColor: ‘red’,
borderRadius: 7,
}
});
|
Screenshots:-