Menus.jsx 975B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import React, { useState } from 'react';
  2. import { useNavigate, useLocation } from "react-router-dom";
  3. import { Menu } from 'antd';
  4. const linkTo = url => {
  5. const a = document.createElement('a');
  6. a.href = item.key;
  7. a.target = '_blank';
  8. a.click();
  9. }
  10. const menuStyle = { height: '100%' };
  11. export default (props) => {
  12. const { theme, items, location } = props;
  13. // const [selectedKeys, setSelectedKeys] = useState([]);
  14. const navigate = useNavigate();
  15. const onClick = (item) => {
  16. // console.log(item);
  17. if (item.key.indexOf('http') === 0) {
  18. // http 开头说明是外部链接
  19. linkTo(item.key);
  20. } else {
  21. // setSelectedKeys([item.key]);
  22. navigate(item.key);
  23. }
  24. }
  25. // React.useEffect(() => {
  26. // if (location.pathname) {
  27. // setSelectedKeys([location.pathname]);
  28. // }
  29. // }, [location.pathname]);
  30. return (
  31. <Menu style={menuStyle} theme={theme} items={items} selectedKeys={[location.pathname]} />
  32. )
  33. }