Menu.Item的问题NavLink始终处于活动状态Semantic UI React

时间:2018-03-20 22:46:12

标签: reactjs react-router semantic-ui semantic-ui-react

我正在使用语义ui反应,我使用NavLink和Menu.Item进行路由。

以下是我的导航栏组件:

const Navbar = () => (
  <div>
    <Container>
      <Menu secondary stackable widths={4}>
        <Menu.Item>
          <img src={Logo} alt="header" />
        </Menu.Item>
        <Menu.Item as={NavLink} to="/" name="home">
          <Icon name="home" size="large" />
          <p>Home</p>
        </Menu.Item>
        <Menu.Item as={NavLink} to="/data" name="data">
          <Icon name="dashboard" size="large" />
          <p>Dashboard</p>
        </Menu.Item>
        <Menu.Item as={NavLink} to="/user" name="user">
          <Icon name="user" size="large" />
          <p>User</p>
        </Menu.Item>
      </Menu>
    </Container>
  </div>
);

现在问题就在这里。路由工作正常,但由于某种原因,“家庭&#39;菜单项始终处于活动状态

然而,其他两条路线工作正常(即只设置&#39;活动&#39;当路线正确时)

Here is what it looks like with /dashboard route active

App.jsx中路由的代码:

<Switch>
    <Route exact path="/" component={Home} />
    <Route path="/data" component={Dashboard} />
    <Route path="/user" component={User} />
</Switch>

上面的照片可以清楚地说明问题所在!

任何输入都将不胜感激!!

2 个答案:

答案 0 :(得分:1)

可以从上面的AngelSalazar确认,这个修复了它。谢谢!

<Menu.Item as={NavLink} exact to="/" name="home">

答案 1 :(得分:0)

这是我用来解决此问题的设置。 我收到以下错误:

Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.
in a (created by Link)
in Link (created by Route)
in Route (created by NavLink)
in NavLink (created by MenuItem)
in MenuItem (at NavBar.js:47)
in a (created by Link)
in Link (created by Route)
in Route (created by NavLink)
in NavLink (at NavBar.js:46)
in div (created by Menu)
in Menu (at NavBar.js:20)
in NavBar (at App.js:26)
in div (at App.js:25)
in Router (created by BrowserRouter)
in BrowserRouter (at App.js:24)
in div (at App.js:22)
in App (at index.js:6)

主要组件设置是在我进行路由的App.js中

 <Router>
      <div>
      <NavBar />
      <Route exact path="/" component={LoginSignUpContainer}></Route>
<Router>

在这里,我有一个NavBar组件,该组件可容纳和发生问题的地方。

class NavBar extends Component{
 constructor(){
    super();

    this.state={
        activeItem: "home"
    }
}
handleItemClick = (e, { name }) => this.setState({ activeItem: name })


render() {
const { activeItem } = this.state

return (
  <Menu>
     <NavLink to="/bills">
    <Menu.Item
      name='bills'
      active={activeItem === 'bills'}
      onClick={this.handleItemClick}>
      <Icon name="payment"/>Bills
    </Menu.Item>

  <Menu>

我删除了NavLink标签,并在添加的Menu.Item标签中:

 as={NavLink}
 to="bills"

更新后:

<Menu.Item
    as={NavLink}
    to="/bills"
    name='bills'
    active={activeItem === 'bills'}
    onClick={this.handleItemClick}>
  <Icon name="payment"/>Login
</Menu.Item>