![](/img/trans.png)
[英]React-Router V4 - Passing Props to Render function in Route Component to generate new component
[英]How do I pass Redux props to a separate component in a different Route with React Router v4?
我正在創建一個帶有React,React Router V4和Redux的實時聊天應用程序。 問題是,您可能知道,反應路由器v4更改了很多東西, 嵌套路由對我不起作用。
所以我有這段代碼與嵌套路由不起作用 :
<Route path='/' component={App}>
<Route path="/user" component={AddUser}/>
</Route>
這是給我這個錯誤的Warning: You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored
Warning: You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored
。
其中component={App}
在第一個<Route path='/'
是一個redux連接:
const App = connect(mapStateToProps, mapDispatchToProps)(Header)
因此,我的組件App
具有我需要的所有props
。 除了需要將這些道具從App
傳遞到嵌套路線組件AddUser
之外,其他所有方法都工作正常。
如何將redux props
傳遞到其他Route
的單獨組件?
當需要嵌套路由時,使用react-router v4時,我們不嵌套路由。 相反,我們將它們放在嵌套組件中。 您可以在此處了解更多信息: 使用react router v4的嵌套路由
在您的情況下,可以將“ /用戶”路由放入App組件內,然后使用render
代替component
。 因此,您可以照常將您的App道具傳遞給AddUser。
<Route path='/' component={App}/>
App.js
class App extends Component{
//...
render(){
return(
//....
<Route path="/user"
render={() => <AddUser myProp={this.props.myAppProp}/>}/>
//....
)
}
}
我找到了閱讀此博客文章的解決方案: https : //medium.com/@5XvYrLT7/react-server-side-rendering-with-react-router-3-x-74cf87919b3
使用react-router v4,您無需再執行此操作,不要嵌套:
<Route component={App}>
<Route component={Index} />
<Route component={About} />
</Route>
您現在就這樣做。 應用程序按原樣嵌入您的路線:
<App>
<Switch>
<Route exact path="/" component={Index} />
<Route path="/about" component={About} />
</Switch>
</App>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.