gpt4 book ai didi

React-Native:错误设置和从ClipBoard获取文本

转载 作者:行者123 更新时间:2023-12-03 16:41:30 33 4
gpt4 key购买 nike

import React, { useState } from 'react'
import { SafeAreaView, View, Text, TouchableOpacity, StyleSheet } from 'react-native'
import Clipboard from '@react-native-community/clipboard'

const App = () => {
const [copiedText, setCopiedText] = useState('')

const copyToClipboard = () => {
Clipboard.setString('hello world')
}

const fetchCopiedText = async () => {
const text = await Clipboard.getString()
setCopiedText(text)
}

return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<TouchableOpacity onPress={() => copyToClipboard()}>
<Text>Click here to copy to Clipboard</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => fetchCopiedText()}>
<Text>View copied text</Text>
</TouchableOpacity>

<Text style={styles.copiedText}>{copiedText}</Text>
</View>

</SafeAreaView>
)
}

const styles = StyleSheet.create({
//styles
})

export default App

当按“复制到剪贴板”时,我收到一条错误消息:
null和对象都不为null('evaluating NativeClipboard_1.default.setString')
并按“查看复制的文本”,我得到TypeError Unhandlded Promise拒绝。
此代码直接从此处复制: https://github.com/react-native-community/clipboard

enter image description here

最佳答案

使用 react 剪贴板

运行示例:https://snack.expo.io/@msbot01/4c673f

代码:

import React, { useState } from 'react'
import { SafeAreaView, View, Text, TouchableOpacity, Clipboard, StyleSheet } from 'react-native'

const App = () => {
const [copiedText, setCopiedText] = useState('')

const copyToClipboard = () => {
Clipboard.setString('hello world')
}

const fetchCopiedText = async () => {
const text = await Clipboard.getString()
setCopiedText(text)
}

return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<TouchableOpacity onPress={() => copyToClipboard()}>
<Text>Click here to copy to Clipboard</Text>
</TouchableOpacity>
<TouchableOpacity style={{marginTop:50}} onPress={() => fetchCopiedText()}>
<Text>Click to View copied text</Text>
</TouchableOpacity>

<Text style={styles.copiedText}>{copiedText}</Text>
</View>

</SafeAreaView>
)
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
copiedText: {
marginTop: 10,
color: 'red'
}
})

export default App

关于React-Native:错误设置和从ClipBoard获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60945656/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com