[英]React: TypeError: Cannot read property 'dishes' of null
I'm pretty new to React and trying to make a project.我对 React 很陌生,并且正在尝试制作一个项目。 However, when I compile, I get an error.
但是,当我编译时,我得到一个错误。 I'm trying to pass the Component on DishdetailComponent to MenuComponent, and the MenuComponent to App.js.
我正在尝试将 DishdetailComponent 上的组件传递给 MenuComponent,并将 MenuComponent 传递给 App.js。 It works when I pass DishDetail to App.js, but it gives an error when I try to pass DishDetail to Menu to App.js.
当我将 DishDetail 传递给 App.js 时它可以工作,但是当我尝试将 DishDetail 传递给 Menu 给 App.js 时它会出错。 The error message occurs on MenuComponent.js, line: 12. Here's my code:
错误消息出现在 MenuComponent.js 的第 12 行。这是我的代码:
DishdetailComponent.js: DishdetailComponent.js:
import React, { Component } from 'react';
import { Media } from 'reactstrap';
import { Card, CardImg, CardImgOverlay, CardText, CardBody,
CardTitle } from 'reactstrap';
class DishDetail extends Component {
constructor(props) {
super(props);
this.state = {
selectedDish: null
}
}
onDishSelect(dish) {
this.setState({ selectedDish: dish});
}
renderDish(dish) {
if (dish != null)
return(
<Card>
<CardImg top src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
);
else
return(
<div></div>
);
}
render() {
const menu = this.props.dishes.map((dish) => {
return (
<div className="col-12 col-md-5 m-1">
<Card key={dish.id}
onClick={() => this.onDishSelect(dish)}>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Card>
</div>
);
});
return (
<div className="container">
<div className="row">
{menu}
</div>
<div className="row">
<div className="col-12 col-md-5 m-1">
{this.renderDish(this.state.selectedDish)}
</div>
</div>
</div>
);
}
}
export default DishDetail;
MenuComponent.js:菜单组件.js:
import React, { Component } from 'react';
import { Media } from 'reactstrap';
import { Card, CardImg, CardImgOverlay, CardText, CardBody, CardTitle } from 'reactstrap';
import DishDetail from './DishdetailComponent';
class Menu extends Component {
constructor(props) {
super(props);
}
render() {
return <DishDetail dishes = {this.state.dishes} />;
}
}
export default Menu;
You are using {this.state.dishes} in MenuComponent.js.您在 MenuComponent.js 中使用 {this.state.dishes}。 But there are no states defined inside the component.
但是组件内部没有定义状态。 I think you are passing down the dishes from App.js to MenuComponent.js.
我认为您正在将菜肴从 App.js 传递给 MenuComponent.js。 If that's the case, you should use {this.props.dishes} as shown below:
如果是这种情况,您应该使用 {this.props.dishes},如下所示:
import React, { Component } from 'react';
import { Media } from 'reactstrap';
import { Card, CardImg, CardImgOverlay, CardText, CardBody, CardTitle } from 'reactstrap';
import DishDetail from './DishdetailComponent';
class Menu extends Component {
constructor(props) {
super(props);
}
render() {
return <DishDetail dishes = {this.props.dishes} />;
}
}
export default Menu;
Also is this the React course from Coursera?这也是 Coursera 的 React 课程吗? I did the same course as well.
我也做了同样的课程。 It's great for beginners.
这对初学者来说很棒。
The problem is that you don´t have the dishes state on MenuComponent.js, you should create it and then pass it to the DishDetail component.问题是您在 MenuComponent.js 上没有菜肴 state,您应该创建它,然后将其传递给 DishDetail 组件。
Example on MenuComponent.js: MenuComponent.js 上的示例:
this.state = {dishes: ['plate', 'glass']}
the problem is you are sending nothing in the return <DishDetail dishes = {this.state.dishes} />;
问题是您在
return <DishDetail dishes = {this.state.dishes} />;
of menuComponent.的菜单组件。
what you need to do is set the value for dishes您需要做的是设置菜肴的价值
try this:尝试这个:
class Menu extends Component {
constructor(props) {
super(props);
this.state = {
dishes: ["abc","xyz"]
}
}
render() {
return <DishDetail dishes = {this.state.dishes} />;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.