簡體   English   中英

反應本機錯誤-違反常態:元素類型無效

[英]React Native Error - Invariant Violation: Element type is invalid

我正在按照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中發布的一些問題,其中大多數問題是有關導出和導入組件的錯誤方法。 據我所知,我在這里正確地導出和導入了組件,但仍然收到錯誤。

如果您需要更多信息,請隨時添加評論。 感謝調試問題的任何幫助。

在App.js和AlbumDetail.js處,導入語句是錯誤的

用它改變

import {View} from "react-native"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM