Tôi đang cố gắng gọi hàm bên trong thành phần con thông qua this.refs nhưng tôi tiếp tục nhận được lỗi rằng hàm này không tồn tại.Phản ứng: Không thể gọi hàm bên trong thành phần con
Uncaught TypeError: this.refs.todayKpi.loadTodaysKpi is not a function
thành phần phụ huynh:
class KpisHeader extends React.Component {
constructor() {
super();
this.onUpdate = this.onUpdate.bind(this);
}
render(){
return <div>
<DateRange ref="dateRange" onUpdate={this.onUpdate}/>
<TodayKpi ref="todayKpi" {...this.state}/>
</div>;
}
onUpdate(val){
this.setState({
startDate: val.startDate,
endDate: val.endDate
}, function(){
this.refs.todayKpi.loadTodaysKpi();
});
}
}
Tôi muốn để có được một số dữ liệu từ phần datarange thông qua chức năng onUpdate, và sau đó tôi muốn kích hoạt một chức năng bên trong TodayKpi mà fetches dữ liệu từ máy chủ. Bây giờ nó chỉ là console.log ("AAA") ;.
thành phần trẻ em:
class TodayKpi extends React.Component {
constructor() {
super();
this.loadTodaysKpi = this.loadTodaysKpi.bind(this);
}
render(){
console.log(this.props.startDate + " "+ this.props.endDate);
return <div className="today-kpi">
</div>;
}
loadTodaysKpi(){
console.log("AAAA");
}
}
Làm thế nào tôi nên thực hiện điều này?