[英]React Native Copy the text loaded via API to Clipboard
I'm Feching the Data from API and I want to Copy that text when the Copy Button is pressed.我正在从 API 获取数据,并且我想在按下复制按钮时复制该文本。
I'm using @react-native-community/clipboard
我正在使用
@react-native-community/clipboard
And showing the data with {item.content}并用 {item.content} 显示数据
I don't know why the text got from api is not copying into clipboard.我不知道为什么从 api 得到的文本没有复制到剪贴板。
This is my app.js file.这是我的 app.js 文件。 Not sure what I'm doing wrong.
不知道我做错了什么。 Thanks.
谢谢。
import React, { useEffect, useState } from "react";
import {
ActivityIndicator,
FlatList,
StyleSheet,
View,
Text,
Button,
} from "react-native";
import Clipboard from '@react-native-community/clipboard';
const App: () => React$Node = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
const [refetch, setRefetch] = useState(false);
const [copiedText, setCopiedText] = useState('');
const copyToClipboard = () => {
Clipboard.setString({item.content});
};
const fetchCopiedText = async () => {
const text = await Clipboard.getString();
setCopiedText(text);
};
useEffect(() => {
fetch("https://exampleapi.com/")
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, [refetch]);
return (
<>
<View style={styles.container}>
{isLoading ? (
<ActivityIndicator />
) : (
<FlatList
data={data}
keyExtractor={({ id }, index) => id}
renderItem={({ item }) => (
<Text style={styles.content}>❝ {item.content} ❞</Text>
)}
/>
)}
</View>
<View>
<View style={styles.buttonStyle}>
<Button
title="New"
onPress={() => setRefetch(!refetch)}
style={styles.buttonCopy}
color="#134074"
/>
</View>
<View style={styles.buttonStyle}>
<Button
title="Copy"
onPress={() => this.copyToClipboard}
style={styles.buttonCopy}
color="#aa4465"
/>
</View>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: "#001524",
padding: 30,
flexDirection: "row",
alignItems: "center"
},
content: {
fontSize: 25,
textAlign: "left",
color: "#ffecd1",
= padding: 15
},
QuotesMark: {
color: "#ffffff",
fontSize: 30
},
buttonStyle: {
padding: 10,
alignItems: "center",
backgroundColor: "#001524",
}
});
export default App;
Fromdocumentation :从文档:
const copyToClipboard = () => {
Clipboard.setString('hello world');
};
It receives string rather than object.它接收字符串而不是 object。
Change your code更改您的代码
const copyToClipboard = () => {
Clipboard.setString({item.content});
};
to this对此
const copyToClipboard = () => {
Clipboard.setString(item.content);
};
Output: Output:
Here is the full working example, here when you click on the FlatList Item, that that item will be copied to the clipboard.这是完整的工作示例,当您单击 FlatList 项目时,该项目将被复制到剪贴板。 I have converted the Copy button to the Paste button to show paste functionality.
我已将复制按钮转换为粘贴按钮以显示粘贴功能。
One more suggestion if you are using Expo for making app, @react-native-community/clipboard
is still not supported by Expo so use Clipboard
provided in react-native
libraries instead, that's what I have done in the following example.如果您使用 Expo 制作应用程序,则还有一个建议,
@react-native-community/clipboard
仍然不受 Expo 支持,因此请使用react-native
库中提供的Clipboard
。
import React, { useEffect, useState } from 'react';
import {
ActivityIndicator,
FlatList,
StyleSheet,
View,
Text,
Button,
TouchableOpacity,
TextInput,
Clipboard,
} from 'react-native';
const App = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
const [refetch, setRefetch] = useState(false);
const [selectedText, setSelectedText] = useState('');
const [copiedText, setCopiedText] = useState(
'Nothing to show, copy by clicking on FlatList Text, and paste it by clicking Paste button'
);
const copyToClipboard = (text) => {
Clipboard.setString(text);
console.log('copied to Clipboard');
fetchCopiedText();
console.log('copied text: ', copiedText);
};
const fetchCopiedText = async () => {
const text = await Clipboard.getString();
setCopiedText(text);
};
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, [refetch]);
return (
<>
<View style={styles.container}>
<TextInput
style={{
padding: 10,
backgroundColor: 'white',
width: 300,
color: 'black',
height: 100,
}}
/>
{isLoading ? (
<ActivityIndicator />
) : (
<FlatList
data={data}
keyExtractor={({ id }, index) => id}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => {
Clipboard.setString(item.title);
console.log('selected text:', selectedText);
}}>
<Text style={styles.content}>❝ {item.title} ❞</Text>
</TouchableOpacity>
)}
/>
)}
</View>
<View style={{ width: 300, height: 100 }}>
<Text>{copiedText}</Text>
</View>
<View>
<View style={styles.buttonStyle}>
<Button
title="New"
onPress={() => setRefetch(!refetch)}
style={styles.buttonCopy}
color="#134074"
/>
</View>
<View style={styles.buttonStyle}>
<Button
title="Paste"
onPress={() => {
fetchCopiedText();
}}
style={styles.buttonCopy}
color="#aa4465"
/>
</View>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#001524',
padding: 30,
flexDirection: 'column',
alignItems: 'center',
},
content: {
fontSize: 25,
textAlign: 'left',
color: '#ffecd1',
},
QuotesMark: {
color: '#ffffff',
fontSize: 10,
},
buttonStyle: {
padding: 10,
alignItems: 'center',
backgroundColor: '#001524',
},
});
export default App;
Working Expo Snack Demo工作Expo 小吃演示
For those who are here using expo and are having issues while using '@react-native-community/clipboard'
.对于那些在这里使用 expo 并且在使用
'@react-native-community/clipboard'
时遇到问题的人。 Switch to expo-clipboard
refer to this documentation切换到
expo-clipboard
参考这个文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.