2017-04-20 44 views
0

react-router - pass props to handler component類似,但在我的情況下,我的組件是在變量中聲明的elem.component,我試圖構建一個傳遞showModal函數的路由數組。傳遞處理程序組件(在變量中定義)

這是我到目前爲止已經試過

routes.push(
    <Route key={elem.to} exact path={elem.to} 
    component={elem.component} showModal={this.props.showModal}/>) 

我一直在尋找的答案,其他的渲染方法,但他們在這我不我的情況下具有的功能定義的組件。

回答

0

使用功能結合

<Route exact path="/" component={() => (<Component prop={val} />) }/>

+0

我的組件是'elem.component'一個變量。 –

0

您必須分配組件大寫變量JSX使用它只是傳遞組件 - 請對此做出何種反應docs約在運行時定義的元素類型的更多細節。請試試這個:

<Route key={elem.to} exact path={elem.to} 
    render={() => { 
     const MyComponent = elem.component; 
     return <MyComponent showModal={someValue} /> 
    }} 

/>