【发布时间】:2019-03-07 03:07:34
【问题描述】:
我有 2 个页面组件,它们有自己的路线。两个页面都有一个标题组件,它应该根据您所在的页面更改其内容。
如何更改每页标题中的内容?
return (
<main className={theme.main}>
<Header
title={this.state.headerTitle}
boldTitle={this.state.boldTitle}
/>
<div className={theme.content}>
<Switch>
<Route path={'/page1'} component={Page1} />
<Route path={'/page2'} component={Page2} />
</Switch>
</div>
<Footer/>
</main>
);
我正在尝试将道具传递给我的路线页面中的标题,如下面的代码所示。
<Route path={'/page2'} component={Page2} currentTitle={this.state.headerTitle} />
更新
我用
import { withRouter } from "react-router"; 和
export default withRouter(Header);
在 Header Component 然后他监听路由,我可以输入路径名来做我需要的事情。
然后,我不需要设置状态,只需监听该 MyComponent 中的路由。
签名:this.props.location
【问题讨论】: