无法在React native中运行自定义组件

时间:2017-06-20 18:35:44

标签: android

需要React Native的帮助。在谷歌搜索,但没有找到任何解决方案。  我有一个适用于android的项目。这里代码:

  import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View
    } from 'react-native';
    export default class MyApp extends Component {
      render() {
        return (
          <View style={styles.container}>
            <Text style={styles.welcome}>
              Welcome to React Native!
            </Text>
            <Text style={styles.instructions}>
              To get started, edit index.android.js
            </Text>
            <Text style={styles.instructions}>
              Double tap R on your keyboard to reload,{'\n'}
              Shake or press menu button for dev menu
            </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,
      },
    });

    AppRegistry.registerComponent('MyApp', () => MyApp);

但是当我尝试添加我自己的组件时,就像这样:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';
import MainPage from "./app/components/MainPage/MainPage";

export default class MyApp extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.android.js
        </Text>
        <Text style={styles.instructions}>
          Double tap R on your keyboard to reload,{'\n'}
          Shake or press menu button for dev menu
        </Text>

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

AppRegistry.registerComponent('MyApp', () => MyApp);

我在模拟器中收到错误: enter image description here

有人可以帮忙吗? 组件MainPage的代码:

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

export default class MainPage extends Component {
    render() {
        return (
            <View>
                <Text style={styles.welcome}>
                    Welcome to React Native!
                </Text>

            </View>
        );
    }
}

MainPage.propTypes = {}

MainPage.defaultProps = {}

const styles = StyleSheet.create({
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    }
});

AppRegistry.registerComponent('MainPage', () => MainPage);

1 个答案:

答案 0 :(得分:1)

我找到解决方案。这是因为文件格式。它应该是.js而不是.jsx。经过这次修正后一切正常。我希望它会帮助某人