简体   繁体   English

简化React导航组件

[英]Simplify React Navigation component

How can I simplify my react navigation component? 如何简化我的反应导航组件? I creaded something like below, but it looks to static and I'm sure it would be better. 我读过类似下面的内容,但是它看起来是静态的,我相信它会更好。

class Nav extends React.Component {
    logout(e) {
        e.preventDefault();
        this.props.addFlashMessage({
            type: 'success',
            text: 'You have been logged out successfully.'
          });
        this.props.logout();
      }

  render() {
    const { isAuthenticated } = this.props.auth;

    const {location} = this.props;
    const homeClass = location.pathname === "/" ? true : false;
    const about = location.pathname.match(/^\/about/) ? true : false;
    const services = location.pathname.match(/^\/services/) ? true : false;
    const loginClass = location.pathname.match(/^\/login/) ? true : false;
    const signupClass = location.pathname.match(/^\/signup/) ? true : false;

    const userLinks = (
      <Menu className="main-menu">
        <MenuItem isActive={homeClass}><Link to="/">Home</Link></MenuItem>
        <MenuItem isActive={AboutClass}><Link to="/about">About</Link></MenuItem>
        <MenuItem isActive={servicesClass}><Link to="/services">Services</Link></MenuItem>
        <MenuItem><Link to="#" onClick={this.logout.bind(this)}>Logout</Link></MenuItem>
      </Menu>
    );

    const guestLinks = (
      <Menu className="main-menu">
        <MenuItem isActive={homeClass}><Link to="/">Home</Link></MenuItem>
        <MenuItem isActive={AboutClass}><Link to="/about">About</Link></MenuItem>
        <MenuItem isActive={servicesClass}><Link to="/services">Services</Link></MenuItem>
        <MenuItem isActive={loginClass}><Link to="/login">Log in</Link></MenuItem>
        <MenuItem isActive={signupClass}><Link to="/signup">Register</Link></MenuItem>
      </Menu>
    );

    return (
      <div className="main-nav">
          <Row>
              <Link to="/" className="site-logo">My App</Link>
              { isAuthenticated ? userLinks : guestLinks }
          </Row>
      </div>
    );
  }
}

I want to do this more dynamic. 我想做得更动态。 What's the better way to do this ? 什么是更好的方法呢? I saw some examples but all were static links like in my component. 我看到了一些示例,但所有示例都是静态链接,例如在我的组件中。

Many thanks for help. 非常感谢您的帮助。

One idea would be to wrap your MenuItem component: 一种想法是包装您的MenuItem组件:

const MenuItemWrapper = (path, title) =>
 <MenuItem isActive={location.pathname.startsWith('/' + path)}>
   <Link to={path}>{title}</Link>
 </MenuItem>

Thus the component could decide if the link should be rendered active or not. 因此,组件可以决定是否应该使链接active

In the next step you could store the links as objects in an array and generate your navigation based on this data: 在下一步中,您可以将链接作为对象存储在数组中,并根据以下数据生成导航:

const menuEntries = [
  { title: 'home', path: '/'},
  { title: 'about', path: '/about'},
  ...
] 

const Links = menuEntries.map(e =>
   <MenuItemWrapper title={e.title} path={e.path} />
)

Thus your menu could be simplified to: 因此,您的菜单可以简化为:

const guestLinks = (
  <Menu className="main-menu">{Links}</Menu>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM