此已被删除.........
您忘了在箭头功能内调用该功能:
onDateChange={(date) => this.props.onDateChange(date)}
要将方法传递给孩子,请删除.bind(this):
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
//some other props
currentDate: moment()
};
componentDidMount() {
this.fetchData();
}
fetchData() {
this.setState({
//set some state here
});
//Some Api call
}
onDateChange(currentDate) {
this.setState({
currentDate
});
}
//doing some stuff to render a table
return (
<div>
<ChildComponent
currentDate={this.state.currentDate}
// Removed .bind(this)
// added missing parameter
onDateChange={(date) => this.onDateChange(date)}
/>
{currentView}
</div>
);
}
render() {
return (
<div>
//renders table
</div>
);
您是否可以共享完整的文件,包括导入类和导出?
从共享的这部分代码中,我可以看到一些问题:
return
(//做一些事情来渲染表格)被放错了位置。您是否要将其放在render
方法中?也许是这样?
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
//some other props
currentDate: moment()
};
}
componentDidMount() {
this.fetchData();
}
fetchData() {
this.setState({
//set some state here
});
//Some Api call
}
onDateChange(currentDate) {
this.setState({
currentDate
});
}
render() {
//doing some stuff to render a table
return (
<>
<div>
<ChildComponent
currentDate={this.state.currentDate}
onDateChange={this.onDateChange.bind(this)}
/>
{currentView}
</div>
<div>
{/* renders table */}
</div>
</>
);
}
}
currentView
这里仍然缺少一些片段,因此,如果您能提供更多背景信息,那将是很好的。
希望对您有帮助。