export default class Root extends React.Component{
constructor(props) {
super(props);
}
dealsCallBack = (dealListFromHome)=>{
console.log("deals from home"+dealListFromHome);
}
render(){
return(
<div>
<Header/>
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/mall/:id" component={MallDetail}/>
<Route path="/home/:location" render={(props) => (<Home passDealsToRoute ={this.dealsCallBack} {...props}/>)} />
<Route exact path="/about" component={About}/>
</Switch>
<Footer/>
</div>
);
}
}
如何从组件Home访问passDealsToRoute? this.props。 passDealsToRoute(theList)无效。
答案 0 :(得分:0)
constructor(props) {
super(props);
this.dealsCallBack = this.dealsCallBack.bind(this);
}
得到了答案。
this.dealsCallBack = this.dealsCallBack.bind(this);
这一行解决了这个问题。