如何将 react-router Link 与 semantic-ui Menu.Item 一起使用?

how to use react-router Link with semantic-ui Menu.Item?

我有这个:

  <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)

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

Material UI 中的每个 Menu.Item 对应一个 onClick props。您可以在 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。参见

您可以在 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>
...