简体   繁体   中英

React Native, element type is invalid: expected a string (for built-in components)

The same issue was already solved in several posts, but non of them helped me (I'm new to React Native, so possibly there is a solution, but I can't find it)

My code uses route and screen. App.js:

import React from 'react';
import { StackNavigator } from 'react-navigation';

import HomeScreen from './screens/HomeScreen.js';

const App = StackNavigator({
  Home: { screen: HomeScreen }
});

HomeScreen.js:

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

export default class HomeScreen extends Component {
  onPressLearnMore() {

  }

  render() {
    return (
      <View style={styles.container}>
        <Button title="Learn More" onPress={() => this.onPressLearnMore()} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

What is wrong?

The project structure:

在此处输入图片说明

您必须在app.js export default App

You don't need to define the file type in your import.

Change this import HomeScreen from './screens/HomeScreen.js';

To this import HomeScreen from './screens/HomeScreen';

Hope it helps.

Edit: Just to gather all information in this answer, as suggested in the comments you should also export your App.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM