反应本机错误-违反常态:元素类型无效

时间:2019-01-29 10:58:01

标签: javascript android reactjs react-native expo

我正在学习Udemy课程,以构建演示React Native应用程序。到目前为止,该应用程序由3个组件组成-App,AlbumList和AlbumDetail。每个代码都在下面列出。


App.js

import React, { Component } from 'react';
import View from 'react-native';
import AlbumDetail from './AlbumDetail'
import axios from 'axios';

class AlbumList extends Component {

  constructor(props){
    super(props)
    this.state = { albums: [{"title": "one"}, {"title": "two"}] }
  }

  renderAlbums() {
    return this.state.albums.map(album =>
      <AlbumDetail key={album.title} album={album} />
    );
  }

  render() {
    return (
      <View>
        { this.renderAlbums() }
      </View>
    );
  }
}

export default AlbumList;

AlbumList.js

import React, { Component } from 'react';
import View from 'react-native';
import AlbumDetail from './AlbumDetail'
import axios from 'axios';

class AlbumList extends Component {

  constructor(props){
    super(props)
    this.state = { albums: [{"title": "one"}, {"title": "two"}] }
  }

  renderAlbums() {
    return this.state.albums.map(album =>
      <AlbumDetail key={album.title} album={album} />
    );
  }

  render() {
    return (
      <View>
        { this.renderAlbums() }
      </View>
    );
  }
}

export default AlbumList;

AlbumDetail.js

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

const AlbumDetail = (props) => {
  return (
    <View>
      <Text>{props.album.title}</Text>
    </View>
    )
};

export default AlbumDetail;

package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "eject": "expo eject"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "expo": "^32.0.0",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.0.0"
  },
  "private": true
}

当我运行npm start并在移动设备(运行Android 8.0.0的One Plus 3T)中启动应用程序时,出现以下错误

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

This error is located at:
    in AlbumList (created by App)
    in RCTView (at View.js:44)
    in App (at withExpoRoot.js:22)
    ...
    ...

我经历了StackOverflow中发布的一些问题,其中大多数问题都涉及导出和导入组件的错误方法。据我所知,我在这里正确地导出和导入了组件,但是仍然出现错误。

如果需要更多信息,请随时添加评论。感谢您调试该问题的任何帮助。

1 个答案:

答案 0 :(得分:4)

在App.js和AlbumDetail.js中,

import语句是错误的

更改

import {View} from "react-native"