简体   繁体   English

如何在 React 中使用导航切换组件?

[英]How to switch components using navigation in React?

I am developing a landing page using React.我正在使用 React 开发登录页面。 It should have the same navigation bar for all sections and change only its active state.它的所有部分都应该具有相同的导航栏,并且仅更改其活动状态。

In App.js file I have used BrowserRouter that routes to Navigation component.在 App.js 文件中,我使用了路由到导航组件的 BrowserRouter。 My problem is that that Navigation does not work properly, it does not switch components, it switches only pathname.我的问题是导航不能正常工作,它不切换组件,它只切换路径名。 For example when I press Cabinet, it should switch from Home to Cabinet component.例如,当我按内阁时,它应该从主页切换到内阁组件。 Should I use another router for Navigation or not?我应该使用另一个路由器进行导航吗? What would be the best solution for this?什么是最好的解决方案?

I have tried to create NavRouter component, where I have placed routers for Navigation.我尝试创建 NavRouter 组件,在其中放置了用于导航的路由器。

App.js file: App.js 文件:

<BrowserRouter>
   <Switch>
        <Route path='/' exact component={ Navigation }></Route>
        <Route path='/cabinet' component={ Navigation }></Route>
        <Route path='/catalog' component={ Navigation }></Route>
        <Route path='/company' component={ Navigation }></Route>
        <Route path='/contacts' component={ Navigation }></Route>
   </Switch>
</BrowserRouter>

Navigation file:导航文件:

class Navigation extends Component {
render() {
const { classes, location: {pathname}, children  } = this.props;
return (
  <div>

  <MuiThemeProvider theme={color}>
  <Fragment>
  <nav id="menu">
  <ul  className="navigation">
  <li>
      <MenuItem component={Link} to='/' selected={'/' === pathname} className="active menuItem" className={classes.menuItemColor}>
          Главная
      </MenuItem>
    </li>
    <li>
      <MenuItem component={Link} to='/cabinet' selected={'/cabinet' === pathname} className="menuItem" className={classes.menuItemColor}>
          Кабинет
      </MenuItem>
      </li>
      <li>
      <MenuItem component={Link} to='/catalog' selected={'/catalog' === pathname} className="menuItem" className={classes.menuItemColor}>
          Каталог
      </MenuItem>
      </li>
      <li>
      <MenuItem component={Link} to='/company' selected={'/company' === pathname } className="menuItem" className="">
          Компания
      </MenuItem>
      </li>
      <li>
      <MenuItem component={Link} to='/contacts' selected={'/contacts' === pathname} className="menuItem" className="">
          Контакты
      </MenuItem>
      </li>
      </ul>

  </nav>
  <main className={classes.content}>
        { children }
    </main>
    </Fragment>
  </MuiThemeProvider>
  </div>
);
 }
}

NavRouter.js file: NavRouter.js 文件:

<BrowserRouter>
     <Navigation>
     <Switch>
         <Route path='/' exact component={ Navigation }></Route>
         <Route path='/home' component={ Home }></Route>
     </Switch>
     </Navigation>
    </BrowserRouter>

you can do the following:您可以执行以下操作:

create a file called Routes.js创建一个名为Routes.js的文件

in Routes.js:在 Routes.js 中:

export default () => (
        <BrowserRouter>
       <Switch>
            <Route path='/' exact component={ Home }></Route>
            <Route path='/cabinet' component={ Cabinet }></Route>
            <Route path='/catalog' component={ Catalog }></Route>
            <Route path='/company' component={ Company }></Route>
            <Route path='/contacts' component={ Contacts }></Route>
       </Switch>
    </BrowserRouter>
)

in App.js在 App.js 中

    import Routes from './Routes';
    import Navigation from './Navigation'
    //inside render
    <Navigation>
        <Routes />
    </Navigation>

Note : to get the current pathName, you have to use a state management solution like Redux.注意:要获取当前路径名,您必须使用像 Redux 这样的状态管理解决方案。

Or : if you are not familiar with redux, for now instead of pathname in Navigation component you can use window.location.pathname或者:如果你不熟悉 redux,现在你可以使用window.location.pathname代替 Navigation 组件中的路径名

Here's the way to you need to do it.这是你需要做的方法。 Separate the header navigation from the other components.将标题导航与其他组件分开。

App.js应用程序.js

   <Navigation/>
   <BrowserRouter>
       <Switch>
            <Route path='/' exact component={ Home }></Route>
            <Route path='/cabinet' component={ Cabinet }></Route>
            <Route path='/catalog' component={ Catalog }></Route>
            <Route path='/company' component={ Company }></Route>
            <Route path='/contacts' component={ Contacts }></Route>
       </Switch>
    </BrowserRouter>

I have solved my problem by creating Root.js file, where I have placed my components我通过创建 Root.js 文件解决了我的问题,我在其中放置了我的组件

class Root extends Component {
render() {
return (
  <div>
    <MainNavigation />
    <TopNav />
    {this.props.children}
    </div>
);
}
}

And in App.js file I have placed this:在 App.js 文件中,我放置了这个:

<BrowserRouter>
        <Root>
          <Route path='/' exact component={ Home } />
          <Route path='/cabinet' component={ Cabinet } />
          <Route path='/catalog' component={ Catalog } />
          <Route path='/company' component={ Company } />
          <Route path='/contacts' component={ Contacts } />
        </Root>
</BrowserRouter>

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

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