【问题标题】:ReferenceError: Can't Find VariableReferenceError:找不到变量
【发布时间】:2018-04-24 04:38:16
【问题描述】:

所以我是 React Native 的新手,但每次我从 Facebook 的 React Native Doc's 中放置一个新的组件(如按钮或图像)时,我总是在模拟器上收到“ReferenceError:Can't Find Variable”错误?当我添加文本以外的任何其他代码时,这确实会发生。我没有错误地链接某些东西吗?任何事情都会有所帮助。

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';

import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

export default class App extends Component<{}> {
  render() {
    return (
        <View style={styles.container}>
        
        <Text style={styles.welcome}>
          This is a new App!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit App.js
        </Text>
        <Text style={styles.instructions}>
          {instructions}
        </Text>
        <Button
  onPress={onPressLearnMore}
  title="Learn More"
  color="#841584"
  accessibilityLabel="Learn more about this purple button"
/>
        <Text style={{color: 'blue'}}>Hello</Text>
      </View>
        
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

**错误 This is the error

【问题讨论】:

  • Text 和 View 是从 react-native 导入的,为什么 Button 没有从任何地方导入?
  • 对不起,我的无知,但每个组件都必须列在导入列表下吗?
  • 不用担心,是的,您想在给定文件中使用的所有内容都需要从其他地方导入或在使用前以某种方式声明。
  • 哦,我明白了,是的,它现在开始工作了!

标签: javascript react-native


【解决方案1】:

尝试像这样将Button 添加到导入中:

import {
  Platform,
  StyleSheet,
  Text,
  View,
  Button
} from 'react-native';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-20
    • 2016-10-30
    • 2012-03-18
    • 2015-11-16
    • 2011-05-22
    • 2014-12-16
    • 2022-01-23
    • 2019-09-16
    相关资源
    最近更新 更多