[英]Is it possible to pass a value from child route to its parent route with react router?
比方說,我有一個標題和正文的MainLayout。 使用react路由器,我在組件中渲染組件。
...
<header></header>
<main>{this.props.children}</main>
...
這是我目前的路由器:
<Route component={MainLayout} onEnter={requireCordova}>
<Route path="/home" component={Home} />
<Route path="/settings" component={Settings} />
</Route>
我的問題是我希望MainLayout從其中一個孩子那里獲取標題(填寫標題),這可能嗎?
<Route component={MainLayout} onEnter={requireCordova}>
<Route path="/home" component={Home} title='homepage' />
<Route path="/settings" component={Settings} title='settings' />
</Route>
您無法直接在組件之間進行通信。 您可能希望使用共享存儲來滿足此類需求 - 最流行的解決方案是Redux 。
為了解決更新標題的具體問題 - 有一個簡單的實用工具react-helmet可以簡化流程。
您可以在任何路徑組件中使用Helmet組件: <Helmet title="My Title" />
和頭盔將負責更新您的標題。 它適用於HTML頭中的其他元素 - 例如。 元,鏈接,腳本標簽等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.