【发布时间】:2020-04-02 02:55:24
【问题描述】:
我最近开始学习 ReactJs,我需要一些帮助。我在我的项目中创建了两个组件。第一个是 MainComponent,它看起来如下:
constructor(props) {
super(props);
this.onDishSelect = this.onDishSelect.bind(this);
this.state = {
dishes: DISHES,
selectedDish: null
};
}
onDishSelect(dishId) {
this.setState({ selectedDish: dishId });
}
作为子组件的另一个组件称为菜单,我使用事件处理程序 onClick 调用父类“Main”中存在的方法“onDishSelect”。这正是我在父组件中所做的,以获取在事件处理程序 Onclick 中调用的方法:
onClick={() => this.onDishSelect(dish)
我将此作为属性添加到子组件中,以使用父组件中的方法。我知道我的代码中缺少一些东西。然而,我处理不了。我想要一种方法来使用方法,而无需在子组件中再次编写此方法。
我很感激任何帮助来解决这个问题,因为我无缘无故地花了很多时间。提前致谢。
附:这是我的主要组件和我的菜单组件代码:
class Main extends Component {
constructor(props) {
super(props);
this.onDishSelect = this.onDishSelect.bind(this);
this.state = {
dishes: DISHES,
selectedDish: null
};
}
onDishSelect(dishId) {
this.setState({ selectedDish: dishId });
}
render() {
return (
<div>
<Navbar dark color="danger">
<div className="container">
<NavbarBrand href="/">Elite Café</NavbarBrand>
</div>
</Navbar>
<Menu
dishes={this.state.dishes}
selectedDish={this.state.selectedDish}
onClick={dishId => this.onDishSelect(dishId)}
/>
</div>
);
}
}
export default Main;
--菜单组件代码:
import React, { Component, Fragment } from "react";
import {
Media,
Card,
CardImg,
CardText,
CardBody,
CardTitle,
CardImgOverlay
} from "reactstrap";
import DishDetail from "./dishDetailsComponent";
class Menu extends Component {
constructor(props) {
super(props);
this.state = {};
}
onDishSelect(dish) {
this.setState({ selectedDish: dish });
}
renderDish(dish) {
if (dish != null) {
return (
<Card className="col-12 col-md-5 col-sm-12 m-1 border-0">
{/* <CardImg width="100%" object src={dish.image} alt={dish.name} />
<CardBody className="cardBody">
<CardTitle>
<strong>{dish.name}</strong>
</CardTitle>
<CardText>{dish.description}</CardText> */}
<DishDetail
className="cardBody"
selectedDish={this.state.selectedDish}
dishes={this.props.dishes}
/>
{/* </CardBody> */}
</Card>
);
} else {
return <div />;
}
}
renderComments(dish) {
if (dish != null) {
return (
<ul className="list-unstyled ml-1">
<div className="col-12 col-md-10">
<li>
<strong>Comments</strong>
</li>
{dish.comments.map((item, id) => (
<div key={id} className="mt-1">
<div className="mb-2">{item.comment}</div>
<li>
{" "}
<div>
-- {item.author}, {item.date}
</div>
</li>
</div>
))}
</div>
</ul>
);
} else {
return <div />;
}
}
render() {
const menu = this.props.dishes.map(dish => {
return (
<div key={dish.id} className="col-12 col-md-5 m-1">
<Card onClick={() => this.onDishSelect(dish)}>
<CardImg width="100%" object src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Card>
</div>
);
});
return (
<div className="container">
<div className="row">
{menu}
{this.renderDish(this.state.selectedDish)}
{this.renderComments(this.state.selectedDish)}
</div>
</div>
);
}
}
export default Menu;
【问题讨论】:
-
你能添加调用孩子的代码部分吗?
-
我编辑了这篇文章。请看一看。
-
您将函数
onDishSelect作为onClick传递给菜单/子组件。如果是类组件,则可以通过 Menu 内的this.props.onClick(dishId)访问,如果 Menu 是功能组件,则可以通过props.onClick(disId)访问。 -
非常感谢您的回复。你能告诉我我应该在哪里添加这一行吗?我有点困惑。
-
它可以添加到您必须访问该功能的子组件中。如果您也可以添加 Menu 组件,将会很有帮助。
标签: reactjs methods components