Javascript 获取null不是使用React Native Clipboard的对象(计算';u.default.setString';) 问题:

Javascript 获取null不是使用React Native Clipboard的对象(计算';u.default.setString';) 问题:,javascript,node.js,reactjs,react-native,expo,Javascript,Node.js,Reactjs,React Native,Expo,当我在snack.expo.io中运行以下示例时,我得到一个错误:null不是一个对象(计算'u.default.setString') 背景 下面是我直接从示例中获取的确切代码: App.js: import React, { useState } from 'react' import { SafeAreaView, View, Text, TouchableOpacity, StyleSheet } from 'react-native' import Clipboard from '@r

当我在snack.expo.io中运行以下示例时,我得到一个错误:
null不是一个对象(计算'u.default.setString')

背景 下面是我直接从示例中获取的确切代码:

App.js:

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

export default function 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({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  copiedText: {
    marginTop: 10,
    color: 'red'
  }
})

谢谢你

此库当前未包含在Expo SDK中。您可以从React Native使用剪贴板:

import { Clipboard } from 'react-native';

我对安卓系统也有同样的问题。 奇怪的是,我不得不安装吊舱:

cd ios && pod install && cd ../
在那之后,我跑了
react native run android
成功了。

Aha!非常感谢你。我正发疯想弄明白为什么它不起作用。
cd ios && pod install && cd ../