index.tsx 1006 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import styles from "./index.module.scss";
  2. import classNames from "classnames";
  3. import history from "@/utils/history";
  4. import { useLocation } from "react-router-dom";
  5. import { useEffect, useState } from "react";
  6. export default function LeftBar({ data }: any) {
  7. const cutRouter = (path: string) => {
  8. history.push(path);
  9. };
  10. const location = useLocation();
  11. const [pathId, setPathId] = useState(1);
  12. useEffect(() => {
  13. const arr = location.pathname.split("/");
  14. let id = 1;
  15. if (arr[2]) id = Number(arr[2]);
  16. setPathId(id);
  17. }, [location]);
  18. return (
  19. <div className={styles.LeftBar}>
  20. {data.length > 0
  21. ? data.map((v: any) => (
  22. <div
  23. onClick={() => cutRouter(v.path)}
  24. className={classNames("leftRow", v.id === pathId ? "active" : "")}
  25. key={v.id}
  26. >
  27. <div></div>
  28. &emsp;{v.name}&emsp;
  29. <div></div>
  30. </div>
  31. ))
  32. : null}
  33. </div>
  34. );
  35. }