UNPKG

5.39 kBMarkdownView Raw
1# @react-native-community/clipboard
2
3[![Build Status][build-badge]][build]
4[![Version][version-badge]][package]
5![Supports iOS and Android][support-badge]
6[![MIT License][license-badge]][license]
7[![Lean Core Badge][lean-core-badge]][lean-core-issue]
8
9
10React Native Clipboard API for both iOS and Android
11
12| iOS | Android |
13| --- | --- |
14| <img src ="https://user-images.githubusercontent.com/6936373/73284520-0ce29880-4238-11ea-9d0e-2061b2d6f17a.png" width="320"/> | <img src ="https://user-images.githubusercontent.com/6936373/73284517-0ce29880-4238-11ea-96c7-5a6337c43da5.png" width="320"/> |
15
16
17
18## Getting started
19Install the library using either Yarn:
20
21```
22yarn add @react-native-community/clipboard
23```
24
25or npm:
26
27```
28npm install --save @react-native-community/clipboard
29```
30
31## Link
32
33- React Native v0.60+
34
35For iOS, use `cocoapods` to link the package.
36
37run the following command:
38
39```
40$ cd ios && pod install
41```
42
43For android, the package will be linked automatically on build.
44
45- React Native <= 0.59
46
47run the following command to link the package:
48
49```
50$ react-native link @react-native-community/clipboard
51```
52
53or you could follow the instructions to [manually link the project](https://reactnative.dev/docs/linking-libraries-ios#manual-linking)
54
55## Upgrading to React Native 0.60+
56
57New React Native comes with `autolinking` feature, which automatically links Native Modules in your project. In order to get it to work, make sure you unlink `Clipboard` first:
58
59```
60$ react-native unlink @react-native-community/clipboard
61```
62
63## Migrating from the core `react-native` module
64This module was created when the Clipboard API was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:
65
66```javascript
67import { Clipboard } from "react-native";
68```
69
70to:
71
72```javascript
73import Clipboard from "@react-native-community/clipboard";
74```
75
76## Example
77
78```jsx
79import React, { useState } from 'react'
80import { SafeAreaView, View, Text, TouchableOpacity, StyleSheet } from 'react-native'
81import Clipboard from '@react-native-community/clipboard'
82
83const App = () => {
84 const [copiedText, setCopiedText] = useState('')
85
86 const copyToClipboard = () => {
87 Clipboard.setString('hello world')
88 }
89
90 const fetchCopiedText = async () => {
91 const text = await Clipboard.getString()
92 setCopiedText(text)
93 }
94
95 return (
96 <SafeAreaView style={{ flex: 1 }}>
97 <View style={styles.container}>
98 <TouchableOpacity onPress={() => copyToClipboard()}>
99 <Text>Click here to copy to Clipboard</Text>
100 </TouchableOpacity>
101 <TouchableOpacity onPress={() => fetchCopiedText()}>
102 <Text>View copied text</Text>
103 </TouchableOpacity>
104
105 <Text style={styles.copiedText}>{copiedText}</Text>
106 </View>
107
108 </SafeAreaView>
109 )
110}
111
112const styles = StyleSheet.create({
113 container: {
114 flex: 1,
115 justifyContent: 'center',
116 alignItems: 'center'
117 },
118 copiedText: {
119 marginTop: 10,
120 color: 'red'
121 }
122})
123
124export default App
125```
126
127# Reference
128
129## Methods
130
131### Clipboard
132
133#### `getString()`
134
135```jsx
136static getString()
137```
138
139Get content of string type, this method returns a `Promise`, so you can use following code to get clipboard content
140
141```jsx
142async _getContent() {
143 var content = await Clipboard.getString();
144}
145```
146
147---
148
149#### `setString()`
150
151```jsx
152static setString(content)
153```
154
155Set content of string type. You can use following code to set clipboard content
156
157```jsx
158_setContent() {
159 Clipboard.setString('hello world');
160}
161```
162
163#### Parameters
164
165| Name | Type | Required | Description |
166| ------- | ------ | -------- | ----------------------------------------- |
167| content | string | Yes | The content to be stored in the clipboard |
168
169
170### useClipboard
171
172`useClipboard` is a utility hooks for the `Clipboard` module. `data` contains the content stored in the clipboard.
173
174```jsx
175import React, { useEffect } from 'react'
176import { Text } from 'react-native'
177import { useClipboard } from '@react-native-community/clipboard'
178
179export const HooksSample = () => {
180 const [data, setString] = useClipboard();
181
182 useEffect(() => {
183 setString('hello world');
184 }, [])
185
186 return (<Text>{data}</Text>)
187}
188
189```
190
191## Maintainers
192
193* [M.Haris Baig](https://github.com/harisbaig100)
194* [Jesse Katsumata](https://github.com/Naturalclar)
195
196## Contributing
197
198Please see the [`contributing guide`](/CONTRIBUTING.md).
199
200## License
201
202The library is released under the MIT licence. For more information see [`LICENSE`](/LICENSE).
203
204[build-badge]: https://img.shields.io/circleci/project/github/react-native-community/clipboard/master.svg?style=flat-square
205[build]: https://circleci.com/gh/react-native-community/clipboard
206[version-badge]: https://img.shields.io/npm/v/@react-native-community/clipboard.svg?style=flat-square
207[package]: https://www.npmjs.com/package/@react-native-community/clipboard
208[support-badge]:https://img.shields.io/badge/platforms-android%20|%20ios-lightgrey.svg?style=flat-square
209[license-badge]: https://img.shields.io/npm/l/@react-native-community/clipboard.svg?style=flat-square
210[license]: https://opensource.org/licenses/MIT
211[lean-core-badge]: https://img.shields.io/badge/Lean%20Core-Extracted-brightgreen.svg?style=flat-square
212[lean-core-issue]: https://github.com/facebook/react-native/issues/23313