我正在学习一门Udemy课程,以构建一个演示react本机应用程序。到目前为止,该应用程序由3个组件组成——应用程序、相册列表和;相册详细信息。下面列出了每个的代码。
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的OnePlus 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中发布的一些问题,其中大多数都涉及导出和导入组件的错误方式。据我所见,我在这里正确地导出和导入了组件,但仍然会出现错误。
如果您需要更多信息,请随时添加评论。如有任何帮助调试此问题,我们将不胜感激。
用更改
import {View} from "react-native"