react 19版中路由react-router-dom v7版的使用

  1. 路由的安装:
javascript 复制代码
npm install react-router-dom

在src目录下建一个router文件夹

在router文件夹里面建一个index.tsx

index.tsx内容:

javascript 复制代码
import React from 'react';
import {
  BrowserRouter as Router,
  Routes,
  Route,
  Link
} from 'react-router-dom';
import ManuList from './router';

interface MenuItem {
  title: string;
  path: string;
  icon?: string;
  iconColor?: string;
  component: React.ComponentType<any>; // 关键修正点:添加泛型参数
}

const IndexRouter: React.FC = () => {
  return (
    <Routes>
      {ManuList.map((item: MenuItem) => (
        <Route
          key={item.path} // 使用 path 作为 key
          path={item.path}
          element={<item.component />} // 使用 element 属性并传递 React 元素
        />
      ))}
    </Routes>
  );
};

export default IndexRouter;

再建一个router.tsx

javascript 复制代码
import Home from '../views/Home';
import Article from '../views/Article';
import Album from '../views/Album';
import LinkPage from '../views/LinkPage';

// 定义菜单项的类型
interface MenuItem {
    title: string;
    path: string;
    icon: string;
    iconColor: string;
    component: React.ComponentType; // 组件类型
}

const ManuList: MenuItem[] = [
    {
        title: '首页',
        path: '/',
        icon: '&#xe628;',
        iconColor: '#ff08f5',
        component: Home
    },
    {
        title: '文章记录',
        path: '/article',
        icon: '&#xe660;',
        iconColor: '#6a00fe',
        component: Article
    },
    {
        title: '相册',
        path: '/album',
        icon: '&#xe9f0;',
        iconColor: '#005efe',
        component: Album
    },
    {
        title: '友情链接',
        path: '/link',
        icon: '&#xe693;',
        iconColor: '#f50707',
        component: LinkPage
    }
];
export default ManuList;

app.tsx

javascript 复制代码
import './App.css'
import { Layout } from 'antd';
import HeaderSide from './common/index/header'
const { Header, Content, Footer } = Layout;
import IndexRouter from './router/index'
function App() {
  return (
    <>
      <Header className='shadow-lg py-8'>
        <HeaderSide />
      </Header>
      <Content>
        <IndexRouter />
      </Content>
      <Footer>
      </Footer>
    </>
  )
}

export default App

main.tsx:

javascript 复制代码
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import 'virtual:uno.css'
import { BrowserRouter } from 'react-router-dom'; // 引入 BrowserRouter
const container = document.getElementById('root');
const root = createRoot(container!); // 使用 createRoot

root.render(
  <StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </StrictMode>
);

import HeaderSide from './common/index/header里面的代码:

javascript 复制代码
import logo from '@/assets/img/logo.svg';
import ManuList from '../../router/router';
import { useNavigate } from 'react-router-dom';
function HeaderSide() {
  const history = useNavigate();

  const handleClick = (path: string) => {
    history(path);
  };
  return (
    <div className="app-header flex items-center">
      <img src={logo} className="App-logo h-64" alt="logo" />
      <ul className="flex items-center">
        {
          ManuList.map((item, index) => {
            return <li 
              key={index} 
              className="mr-25 text-18 ml-20 cursor-pointer hover:text-blue-500"
              onClick={() => handleClick(item.path)}>
              <span 
                className="iconfont mr-5"
                style={{ color: item.iconColor }}  
                dangerouslySetInnerHTML={{ __html: item.icon }}></span>
              {item.title}
            </li>
          })
        }
      </ul>
    </div>
  )
}
export default HeaderSide

注:在 React Router v7 中,不再使用 useHistory 钩子,React Router 采用了新的 API,其中包括使用 useNavigate 钩子来代替 useHistory。

相关推荐
崔庆才丨静觅2 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60613 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了3 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅3 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅3 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅4 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment4 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅4 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊4 小时前
jwt介绍
前端
爱敲代码的小鱼4 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax