让路由器4在路线中通过参数

时间:2017-06-16 16:58:28

标签: reactjs react-router react-router-v4

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)无效。

1 个答案:

答案 0 :(得分:0)

constructor(props) {
    super(props);
    this.dealsCallBack = this.dealsCallBack.bind(this);
  }

得到了答案。 this.dealsCallBack = this.dealsCallBack.bind(this);这一行解决了这个问题。