我对 ReactJS 比较陌生,刚刚开始制作一个投资组合项目。
当我运行该程序时,它不会在编辑器中显示任何错误,而是在浏览器中显示以下错误。
错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
检查
HomePage
.
这是我的HomePage.js
import React from 'react';
import Hero from '../components/Hero';
import Carousel from '../components/Carousel';
function HomePage(props) {
return(
<div>
<Hero title={props.title} subTitle={props.subTitle} text={props.text} />
<Carousel />
</div>
);
}
export default HomePage;
Hero.js
import React from 'react';
import Jumbotron from 'react-bootstrap/Jumbotron';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
function Hero(props) {
return(
<Jumbotron className="bg-transparent jumbotron-fluid p-0">
<Container fluid={true}>
<Row className="justify-content-center py-5">
<Col md={8} sm={12}>
{ props.title && <h1 className="display-1 font-weight-bolder">{props.title}</h1> }
{ props.subTitle && <h3 className="display-4 font-weight-light">{props.subTitle}</h3> }
{ props.text && <h3 className="lead font-weight-light">{props.text}</h3> }
</Col>
</Row>
</Container>
</Jumbotron>
);
}
export default Hero;
轮播.js
import React from 'react';
import Card from '../components/Card';
import pose1 from '../assets/images/pose1.jpg';
import pose2 from '../assets/images/pose2.jpg';
import pose3 from '../assets/images/evverest.jpg';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
class Carousel extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [
{
id: 0,
title: 'Dev Grub',
subTitle: 'The cookbook for developers',
imgSrc: pose1,
link: 'https://devgrub.com',
selected: false
},
{
id: 1,
title: 'Garrett Love',
subTitle: 'YouTube channel',
imgSrc: pose2,
link: 'https://www.youtube.com/channel/UCxSITxL2JbF229OGCqieVZw',
selected: false
},
{
id: 2,
title: 'Evverest',
subTitle: 'A social network for developers',
imgSrc: pose3,
link: 'https://github.com/garrettlove8/evverest',
selected: false
},
]
}
}
handleCardClick = (id, card) => {
let items = [...this.state.items];
items[id].selected = items[id].selected ? false : true;
items.forEach(item => {
if(item.id !== id) {
item.selected = false;
}
});
this.setState({
items
});
}
makeItems = (items) => {
return items.map(item => {
return <Card item={item} click={(e => this.handleCardClick(item.id, e))} key={item.id} />
})
}
render() {
return(
<Container fluid={true}>
<Row className="justify-content-around">
{this.makeItems(this.state.items)}
</Row>
</Container>
);
}
}
export default Carousel;
进口是绝对正确的。如何解决此错误?
输出/控制台: