2017-06-16 151 views
0
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> 
    ); 
    } 
} 

如何从组件主页访问passDealsToRoute? this.props。 passDealsToRoute(theList)不起作用。反应路由器4在路线中传递参数

+0

你的要求是一样的这个问题https://stackoverflow.com/questions/44255415/react-router-4-1-1-this-props-route-is-undefined/44255850#44255850,唯一的区别是主组件在两个不同的路由中被调用,第一个组件不提供该道具,因此在这种情况下可能会失败。试着在使用前提供一个支票 –

+0

我的版本是一样的4.1.1,但我没有得到道具。 passDealsToRoute,这也是一个回调。 –

+0

@ShubhamKhatri,这就是为什么我使用精确的匹配确切的网址(都带有参数和W/O PARAMS) –

回答

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

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