由于我是React Native的新手,因此我將盡可能詳細(xì)地說(shuō)明這一點(diǎn),也許這也會(huì)對(duì)其他人有所幫助?。我正在使用React Native超級(jí)網(wǎng)格庫(kù),該庫(kù)內(nèi)部使用FlatList和SectionList來(lái)呈現(xiàn)網(wǎng)格布局。網(wǎng)格通過(guò)來(lái)自單獨(dú)const的props填充文本我正在嘗試做的是:當(dāng)用戶點(diǎn)擊FlatList中的任何項(xiàng)目時(shí),被點(diǎn)擊的項(xiàng)目將被復(fù)制到剪貼板,并顯示一條警報(bào),確認(rèn)已被復(fù)制?,F(xiàn)在發(fā)生的事情:每個(gè)項(xiàng)目都是可輕敲的,并且正確的警報(bào)顯示在onPress上,確認(rèn)您已復(fù)制到剪貼板,但實(shí)際上沒(méi)有任何內(nèi)容寫(xiě)入剪貼板。為了確保writeToClipboard正常工作,我在其中有一條靜態(tài)消息,上面寫(xiě)著“至少可以使剪貼板工作正?!?,因此,如果您輕按任何一項(xiàng),則該靜態(tài)消息將被成功復(fù)制到剪貼板。我只是不確定如何將被點(diǎn)擊的特定項(xiàng)目復(fù)制到剪貼板。這是網(wǎng)格組件的代碼:import React, { Component } from "react";import { StyleSheet, Alert, View, Text, TouchableOpacity, Clipboard, Button, onPress} from "react-native";import { FlatGrid } from "react-native-super-grid";class Emojigrid extends Component { constructor(props) { super(props); this.state = { text: "WELL AT LEAST THE CLIPBOARD WORKS", clipboardContent: null }; } writeToClipboard = async () => { await Clipboard.setString(this.state.text); alert("Boom, Copied"); }; render() { return ( <FlatGrid itemDimension={130} items={items} style={styles.gridView} // staticDimension={300} // fixed spacing={2} renderItem={({ item, index }) => ( <View style={[styles.itemContainer, { backgroundColor: "#F7F7F7" }]}> <TouchableOpacity onPress={this.writeToClipboard}> <Text style={styles.itemName}>{item.name}</Text> </TouchableOpacity> </View> )} /> ); }}export default Emojigrid;const items = [ { name: "ˉ_(ツ)_/ˉ" }, { name: "?·??·?" }, { name: "?`_′?" },];const styles = StyleSheet.create({ gridView: { marginTop: 0, marginBottom: 400, flex: 1 }, itemContainer: { justifyContent: "center", alignItems: "center", borderRadius: 0, height: 125 }, itemName: { fontSize: 18, color: "black", fontWeight: "400" }});認(rèn)為答案可能很明顯,但是任何幫助都將不勝感激!
如何將FlatList文本項(xiàng)復(fù)制到Clipboard onPress
明月笑刀無(wú)情
2021-05-08 11:09:00