Reactjs 当用户导航到React中的其他组件时,更改应用程序栏中的标题
我有一个React应用程序,它有一系列组件,并按以下方式进行结构Reactjs 当用户导航到React中的其他组件时,更改应用程序栏中的标题,reactjs,material-design,Reactjs,Material Design,我有一个React应用程序,它有一系列组件,并按以下方式进行结构 class App extends Component { render() { return ( <Router> <ResponsiveDrawer> <div className="App"> <div> <Route exact path="/" co
class App extends Component {
render() {
return (
<Router>
<ResponsiveDrawer>
<div className="App">
<div>
<Route exact path="/" component={Administrator} />
<Route path="/admin" component={Administrator} />
<Route exact path="/jobs" component={Jobs} />
<Route path="/jobs/:id" render={({match}) => <ViewJob id={match.params.id} />} />
<Route path="/entries/:jobId/:entryId" render={({match}) => <Entry jobId={match.params.jobId} entryId={match.params.entryId} />} />
<Route exact path="/reports" component={Reports} />
<Route path="/reports/:jobId" render={({match}) => <JobReport id={match.params.jobId} />} />
<Route path="/reports/activity" component={ActivityReport} />
</div>
</div>
</ResponsiveDrawer>
</Router>
);
}
}
export default App;
类应用程序扩展组件{
render(){
返回(
} />
} />
} />
);
}
}
导出默认应用程序;
ResponsiveDrawer的一部分包含一个标题,我想根据用户当前所在的组件更改该标题。有什么方法可以像这样将道具传递给父级吗?如果您没有使用redux,可以基于location.pathname在ResponsiveDrawer组件的componentDidMount中添加switch case语句。如果您没有使用redux,您可以根据location.pathname在ResponsiveDrawer组件的componentDidMount中添加switch case语句。您可以使用以下方法
ResponsiveDrawer
。因此,您可以从当前URL映射ResponsiveDrawer
组件中的页面标题redux
,则可以从每个组件更新存储中的全局标题变量。然后连接ResponsiveDrawer
以存储和显示标题您可以使用以下方法
ResponsiveDrawer
。因此,您可以从当前URL映射ResponsiveDrawer
组件中的页面标题redux
,则可以从每个组件更新存储中的全局标题变量。然后连接ResponsiveDrawer
以存储和显示标题也许您可以使用
this.context.router
尝试类似的方法
class App extends Component {
findComponent = () => {
switch (this.context.router.history.location.pathname.split('/')[0]) {
case 'admin':
return 'Administrator';
// ...
default:
return 'Administrator';
}
}
render() {
return (
<Router>
<ResponsiveDrawer component={this.findComponent()}>
{/* Your routes */}
</ResponsiveDrawer>
</Router>
);
}
}
App.contextTypes = {
router: PropTypes.object.isRequired,
};
类应用程序扩展组件{
findComponent=()=>{
交换机(this.context.router.history.location.pathname.split('/')[0]){
案例“管理”:
返回“管理员”;
// ...
违约:
返回“管理员”;
}
}
render(){
返回(
{/*您的路线*/}
);
}
}
App.contextTypes={
路由器:PropTypes.object.isRequired,
};
也许您可以使用this.context.router
尝试类似的方法
class App extends Component {
findComponent = () => {
switch (this.context.router.history.location.pathname.split('/')[0]) {
case 'admin':
return 'Administrator';
// ...
default:
return 'Administrator';
}
}
render() {
return (
<Router>
<ResponsiveDrawer component={this.findComponent()}>
{/* Your routes */}
</ResponsiveDrawer>
</Router>
);
}
}
App.contextTypes = {
router: PropTypes.object.isRequired,
};
类应用程序扩展组件{
findComponent=()=>{
交换机(this.context.router.history.location.pathname.split('/')[0]){
案例“管理”:
返回“管理员”;
// ...
违约:
返回“管理员”;
}
}
render(){
返回(
{/*您的路线*/}
);
}
}
App.contextTypes={
路由器:PropTypes.object.isRequired,
};
谢谢!如果我希望标题与URL不同,这将如何工作。例如,我的一些url是products/editproduct/:id,我不想将其公开给用户。您可以将url映射到ResponsiveDrawer组件中的标题。谢谢!如果我希望标题与URL不同,这将如何工作。例如,我的一些url是products/editproduct/:id,我不想将其公开给用户。您可以将url映射到ResponsiveDrawer组件中的标题。谢谢您的想法。我得到一个错误:TypeError:无法读取未定义的属性“pathname”感谢这个想法。我得到错误:TypeError:无法读取未定义的属性“pathname”