2017-07-25 129 views
1

我傳遞一個道具反應路線組件如下所列:陣營:道具不會顯示在陣營組件

<Route exact 
     path='/' 
     component={HomePage} 
     display_notification={this.display_notification.bind(this)} 
/> 

但是當我打印首頁的道具我找不到display_notification。

請幫忙。

我使用的反應路由器-V4

回答

4

路線不重構道具給你,它只能通過內部位置相關的道具。您需要使用render={} API - https://reacttraining.com/react-router/web/api/Route/render-func

所以像

<Route exact 
    path='/' 
    render={() => <HomePage display_notification={this.display_notification} />} /> 
+0

,但如果我這樣做,比我無法在組件使用道具訪問的位置。 – sam23

+0

他們到達作爲參數傳遞給渲染FUNC - 所以'{道具=><首頁富= {} props.bar通知= {} this.blah />' –

+0

@ sam23,你需要用與withRouter您的組件,檢查了這問題https://stackoverflow.com/questions/44127739/programatically-routing-based-on-a-condition-with-react-router/44128108#44128108 –