目录
文章目录
前言
1.一级路由
2.重定向
4.useRoutes()
5.嵌套路由
6.
7.函数组件params参数的使用
使用useParams()
8.函数式组件search参数
useSearchParams()
9. useLocation()
10.编程式路由导航
总结
这里汇总了React新路由的使用方法,个人学习记录笔记,有什么不正确请指出
区别:
1.这里由之前的 component={About} 变成 element={
2.
3.
{/* 路由链接 */}
About
Home
{/* 注册路由 */}
//注意:这里由之前的component={About}变成element={ }
//并且:替代了 ,并且 必须被包裹着
}/>
}/>
import React,{useState} from 'react'
import {Navigate} from 'react-router-dom'
export default function Home() {
const [sum,setSum] = useState(1)
return (
我是Home的内容
{/* 根据sum的值决定是否切换视图 */}
{sum === 1 ? sum的值为{sum}
: }
)
}
{/* 注册路由 */}
}/>
}/>
}/>
// 注意: NavLink默认类名是active,下面是指定自定义的class
//自定义样式
{
console.log('home', isActive)
return isActive ? 'base one' : 'base'
}}
>login
/*
默认情况下,当Home的子组件匹配成功,Home的导航也会高亮,
当NavLink上添加了end属性后,若Home的子组件匹配成功,则Home的导航没有高亮效果。
*/
home
//路由表配置:src/routes/index.js
import About from '../pages/About'
import Home from '../pages/Home'
import {Navigate} from 'react-router-dom'
export default [
{
path:'/about',
element:
},
{
path:'/home',
element:
},
{
path:'/',
element:
}
]
//使用
//App.jsx
import React from 'react'
import {NavLink,useRoutes} from 'react-router-dom'
import routes from './routes'
export default function App() {
//根据路由表生成对应的路由规则
const element = useRoutes(routes)
return (
......
{/* 注册路由 */}
{element} //这里这样写只能匹配一级路由,子路由不行
......
)
}
import About from '../pages/About'
import Home from '../pages/Home'
import Message from '../pages/Message'
import News from '../pages/News'
import {Navigate} from 'react-router-dom'
export default [
{
path:'/about',
element:
},
{
path:'/home',
element: ,
children:[
{
path:'news',
element:
},
{
path:'message',
element:
}
]
},
{
path:'/',
element:
}
]
//根据路由表生成对应的路由规则
const element = useRoutes([
{
path:'/about',
element:
},
{
path:'/home',
element: ,
children:[
{
path:'news',
element:
},
{
path:'message',
element: ,
}
]
}
])
//Home.js
import React from 'react'
import {NavLink,Outlet} from 'react-router-dom'
export default function Home() {
return (
Home组件内容
-
News
-
Message
{/* 指定路由组件呈现的位置 */}
)
}
import React from 'react';
import { Routes, Route, useParams } from 'react-router-dom';
import User from './pages/User.jsx'
function ProfilePage() {
// 获取URL中携带过来的params参数
let { id } = useParams();
}
function App() {
return (
}/>
);
}
import React from 'react'
import {useSearchParams} from 'react-router-dom'
export default function Detail() {
const [search,setSearch] = useSearchParams()
const id = search.get('id')
const title = search.get('title')
const content = search.get('content')
return (
-
- 消息编号:{id}
- 消息标题:{title}
- 消息内容:{content}
)
}
import React from 'react'
import {useLocation} from 'react-router-dom'
export default function Detail() {
const x = useLocation()
console.log('@',x)
// x就是location对象,简写例子:
/*
{
hash: "",
key: "ah9nv6sz",
pathname: "/login",
search: "?name=zs&age=18",
state: {a: 1, b: 2}
}
*/
const {state:{id,title,content}} = useLocation()
return (
- 消息编号:{id}
- 消息标题:{title}
- 消息内容:{content}
)
}
import React from 'react'
import {useNavigate} from 'react-router-dom'
export default function Demo() {
const navigate = useNavigate()
const handle = () => {
//第一种使用方式:指定具体的路径
navigate('/login', {
replace: false,
state: {a:1, b:2}
})
//第二种使用方式:传入数值进行前进或后退,类似于5.x中的 history.go()方法
navigate(-1)
}
return (
)
}
本文仅仅简单介绍了React路由6的使用,可以快速了解新路由实现