我正在从 API 获取数据,并且我想在按下复制按钮时复制该文本。
我正在使用 @react-native-community/clipboard
并用 {item.content} 显示数据
我不知道为什么从 api 获得的文本没有复制到剪贴板。
这是我的 app.js 文件。不知道我做错了什么。谢谢。
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;
来自 documentation :
它接收字符串而不是对象。
更改您的代码
对此