在app.js中导入自定义组件ReferenceError找不到变量:说明

时间:2019-03-29 09:05:34

标签: react-native

我是本机反应的新手,并尝试构建一些基本的应用程序。我试图在我的app.js中呈现一个名为“ Home”的自定义组件,但收到错误:ReferenceEroor:找不到variable:instructions。我坚信这是添加自定义组件最简单的方法之一,直到出现此错误。是否有人可以进一步帮助我?

代码app.js:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import Home from './src/Home';

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.main}>
          <Home/>
      </View>

    );
  }
}

const styles = StyleSheet.create({
  main: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  }

});

代码Home.js:

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


const Home = () => (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to Picoo!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
      )


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

export default Home;

收到此错误: enter image description here

1 个答案:

答案 0 :(得分:1)

您正在尝试访问在Home组件文件中声明的instructions变量...并且在其他作用域中声明了该变量

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',
});