首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在更改URL时在导航栏中添加/删除新页签?

如何在更改URL时在导航栏中添加/删除新页签?
EN

Stack Overflow用户
提问于 2019-04-29 04:33:50
回答 1查看 83关注 0票数 0

新的反应。我有一个反应组件Navbar.js,将显示在3个页面:登陆,登录和主页,但在每页不同的标签。

例如,它将在登陆中显示登录按钮选项卡,但会隐藏在登录页面中,而在主页中将显示搜索框和注销按钮。

通过测试URL,我尝试在从登录页面转到登录页面时隐藏菜单图标:

代码语言:javascript
复制
const opendrawer = (
      <IconButton
        className={classes.menuButton}
        color="inherit"
        aria-label="Open drawer"
      >
        <MenuIcon />
      </IconButton>
    );
代码语言:javascript
复制
 return (
      <div className={classes.root}>
        <AppBar position="static">
          <Toolbar>

{window.location.href.includes("/login") ? null : opendrawer}


            </div>
          </Toolbar>
        </AppBar>

在尝试之后,菜单图标确实隐藏了,但只有当我手动刷新页面时才会隐藏。

EN

回答 1

Stack Overflow用户

发布于 2019-04-29 20:26:14

你可以使用道具来实现这一点,创建一个常量来通知你想要呈现这个特定图标的元素。我在我的应用程序上做了类似的事情,我只想在一些页面中呈现一个底部栏:

App.js

代码语言:javascript
复制
const tasks = {
  task1: {
    bottombar: true,
    // more features you want to turn on or off
  },
  task2: {
    bottombar: false,
  },

// Route is the React Component used for routing,
// Canvas is the component I use to draw the main pages of my app
// the {...props} passes all the feature configuration I previously did
// the module segment I pass the module to render, in this case those two tasks
<Route path="(/task1)" render={(props) => <Canvas {...props} module={tasks.task1} />} />
<Route path="(/task2)" render={(props) => <Canvas {...props} module={tasks.task2} />} />

Canvas.js

代码语言:javascript
复制
render() {
  return (
     // it will check if the props I passed here is true and render the component 
     // or the feature I want, If it is false, it will render nothing, undefined
     {this.props.module.bottombar ? <BottomBar {...this.props} selectedElement={this.state.selectedElement} /> : undefined}
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55894355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档