如何使用react-route Link与语义菜单.Item?

时间:2017-10-16 11:20:48

标签: reactjs react-router semantic-ui

我有这个:

  <Link to="/">
    <Menu.Item name='expense List' active={activeItem === 'expense List'} onClick={this.handleItemClick} />
  </Link>

但我在控制台中收到错误:

Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.
    in a (created by MenuItem)
    in MenuItem (at Header.js:26)
    in a (created by Link)
    in Link (at Header.js:25)
    in div (created by Menu)
    in Menu (at Header.js:22)
    in div (at Header.js:20)
    in Header (at AddExpense.js:8)
    in div (at AddExpense.js:7)
    in AddExpense (created by Route)
    in Route (at index.js:20)
    in Switch (at index.js:18)
    in Router (created by BrowserRouter)
    in BrowserRouter (at index.js:17)
    in Routes (at index.js:30)

我应该如何正确定义我的链接?

2 个答案:

答案 0 :(得分:0)

物料用户界面中的每个onClick props都有一个Menu.Item。您可以在onClick处理程序中使用this.props.history.push('/')

<Menu.Item
  name='expense List'
  active={activeItem === 'expense List'}
  onClick={() => this.props.history.push('/')} />
  // You can also define the function outside and call history.push there

您可能希望使用withRouter。见How to push to History in React Router v4?

答案 1 :(得分:0)

您只能在SemanticUI组件中使用as道具。

...
import { Link } from "react-router-dom";
import { Menu } from "semantic-ui-react";

...
<Menu.Menu>
   <Menu.Item as={Link} to="/path">Click me</Menu.Item>
</Menu.Menu>
...