目录:
概述
About.jsx
import React from 'react';
function About(props) {
return (
我是About的内容
);
}
export default About;
Home.jsx
import React from 'react';
function Home(props) {
return (
我是Home的内容
);
}
export default Home;
App.js
import React from 'react';
import {NavLink, Routes, Route} from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
function App(props) {
return (
React Router Demo
About
Home
}>
}>
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {BrowserRouter} from "react-router-dom";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
);
App.js
import React from 'react';
import {NavLink, Routes, Route, Navigate} from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
function App(props) {
return (
React Router Demo
About
Home
}>
}>
}>
);
}
export default App;
Home.jsx
import React, {useState} from 'react';
import {Navigate} from "react-router-dom";
function Home(props) {
const [sum, setSum] = useState(1)
return (
我是Home的内容
{sum === 2 ? : 当前sum的值是:{sum}
}
);
}
export default Home;
App.js
import React from 'react';
import {NavLink, Routes, Route, Navigate} from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
function App(props) {
function computedClassName({isActive}) {
return isActive ? 'list-group-item atguigu' : 'list-group-item'
}
return (
React Router Demo
About
Home
}>
}>
}>
);
}
export default App;
About.jsx
import React from 'react';
function About(props) {
return (
我是About的内容
);
}
export default About;
Home.jsx
import React from 'react';
function Home(props) {
return (
我是Home的内容
);
}
export default Home;
route/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.js
import React from 'react';
import {NavLink, useRoutes} from "react-router-dom";
import routes from "./routes";
function App(props) {
const element = useRoutes(routes)
return (
React Router Demo
About
Home
{/**/}
{/* }> */}
{/* }> */}
{/* }>*/}
{/* */}
{element}
);
}
export default App;
App.js
import React from 'react';
import {NavLink, useRoutes} from "react-router-dom";
import routes from "./routes";
function App(props) {
const element = useRoutes(routes)
return (
React Router Demo
About
Home
{/**/}
{/* }> */}
{/* }> */}
{/* }>*/}
{/* */}
{element}
);
}
export default App;
route/index.js
import About from "../pages/About";
import Home from "../pages/Home";
import {Navigate} from "react-router-dom";
import News from "../pages/News";
import Message from "../pages/Message";
export default [
{
path: '/about',
element:
},
{
path: '/home',
element: ,
children: [
{
path: 'news',
element:
},
{
path: 'message',
element:
}
]
},
{
path: '/',
element:
},
]
News.jsx
import React from 'react';
function News(props) {
return (
- news001
- news002
- news003
);
}
export default News;
Message.jsx
import React from 'react';
function Message(props) {
return (
);
}
export default Message;
Home.jsx
import React from 'react';
import {NavLink, Outlet} from "react-router-dom";
function Home(props) {
return (
Home组件内容
-
News
{/*News */}
{/*News */}
-
Message
{/*Message */}
{/*Message */}
);
}
export default Home;
About.jsx
import React from 'react';
function About(props) {
return (
我是About的内容
);
}
export default About;
Detail.jsx
import React from 'react';
import {useMatch, useParams} from "react-router-dom";
function Detail(props) {
const {id, title, content} = useParams()
const x = useMatch('/home/message/detail/:id/:title/:content')
console.log(x)
return (
- 消息编号:{id}
- 消息标题:{title}
- 消息内容:{content}
);
}
export default Detail;
Message.jsx
import React, {useState} from 'react';
import {Link, Outlet} from "react-router-dom";
function Message(props) {
const [messages] = useState([
{id: '001', title: '消息1', content: '锄禾日当午'},
{id: '002', title: '消息2', content: '汗滴禾下土'},
{id: '003', title: '消息3', content: '谁知盘中餐'},
{id: '004', title: '消息4', content: '粒粒皆辛苦'},
])
return (
{
messages.map((m) => {
return (
-
{m.title}
)
})
}
);
}
export default Message;
routes/index.js
import About from "../pages/About";
import Home from "../pages/Home";
import {Navigate} from "react-router-dom";
import News from "../pages/News";
import Message from "../pages/Message";
import Detail from "../pages/Detail";
export default [
{
path: '/about',
element:
},
{
path: '/home',
element: ,
children: [
{
path: 'news',
element:
},
{
path: 'message',
element: ,
children: [
{
path: 'detail/:id/:title/:content',
element:
}
]
}
]
},
{
path: '/',
element:
},
]
Detail.jsx
import React from 'react';
import {useLocation, useSearchParams} from "react-router-dom";
function Detail(props) {
const [search, setSearch] = useSearchParams()
const id = search.get('id')
const title = search.get('title')
const content = search.get('content')
const x = useLocation()
console.log(x)
return (
-
- 消息编号:{id}
- 消息标题:{title}
- 消息内容:{content}
);
}
export default Detail;
Message.jsx
import React, {useState} from 'react';
import {Link, Outlet} from "react-router-dom";
function Message(props) {
const [messages] = useState([
{id: '001', title: '消息1', content: '锄禾日当午'},
{id: '002', title: '消息2', content: '汗滴禾下土'},
{id: '003', title: '消息3', content: '谁知盘中餐'},
{id: '004', title: '消息4', content: '粒粒皆辛苦'},
])
return (
{
messages.map((m) => {
return (
-
{/*{m.title} */}
{m.title}
)
})
}
);
}
export default Message;
index.js
import About from "../pages/About";
import Home from "../pages/Home";
import {Navigate} from "react-router-dom";
import News from "../pages/News";
import Message from "../pages/Message";
import Detail from "../pages/Detail";
export default [
{
path: '/about',
element:
},
{
path: '/home',
element: ,
children: [
{
path: 'news',
element:
},
{
path: 'message',
element: ,
children: [
{
path: 'detail',
element:
}
]
}
]
},
{
path: '/',
element:
},
]
Detail.jsx
import React from 'react';
import {useLocation} from "react-router-dom";
function Detail(props) {
const {state: {id, title, content}} = useLocation()
return (
- 消息编号:{id}
- 消息标题:{title}
- 消息内容:{content}
);
}
export default Detail;
Message.jsx
import React, {useState} from 'react';
import {Link, Outlet} from "react-router-dom";
function Message(props) {
const [messages] = useState([
{id: '001', title: '消息1', content: '锄禾日当午'},
{id: '002', title: '消息2', content: '汗滴禾下土'},
{id: '003', title: '消息3', content: '谁知盘中餐'},
{id: '004', title: '消息4', content: '粒粒皆辛苦'},
])
return (
{
messages.map((m) => {
return (
-
{/*{m.title} */}
{/*{m.title}*/}
{m.title}
)
})
}
);
}
export default Message;
routes/index.js
import About from "../pages/About";
import Home from "../pages/Home";
import {Navigate} from "react-router-dom";
import News from "../pages/News";
import Message from "../pages/Message";
import Detail from "../pages/Detail";
export default [
{
path: '/about',
element:
},
{
path: '/home',
element: ,
children: [
{
path: 'news',
element:
},
{
path: 'message',
element: ,
children: [
{
path: 'detail',
element:
}
]
}
]
},
{
path: '/',
element:
},
]
Header.jsx
import React from 'react';
import {useNavigate} from "react-router-dom";
function Header(props) {
const navigate = useNavigate()
function back() {
navigate(-1)
}
function forward() {
navigate(1)
}
return (
React Router Demo
);
}
export default Header;
Message.jsx
import React, {useState} from 'react';
import {Link, Outlet, useNavigate} from "react-router-dom";
function Message(props) {
const [messages] = useState([
{id: '001', title: '消息1', content: '锄禾日当午'},
{id: '002', title: '消息2', content: '汗滴禾下土'},
{id: '003', title: '消息3', content: '谁知盘中餐'},
{id: '004', title: '消息4', content: '粒粒皆辛苦'},
])
const navigate = useNavigate()
function showDetail(m) {
navigate('detail', {replace: false, state: {id: m.id, title: m.title, content: m.content}})
}
return (
{
messages.map((m) => {
return (
-
{/*{m.title} */}
{/*{m.title}*/}
{m.title}
)
})
}
);
}
export default Message;
App.js
import React from 'react';
import {NavLink, useRoutes} from "react-router-dom";
import routes from "./routes";
import Header from "./components/Header";
function App(props) {
const element = useRoutes(routes)
return (
About
Home
{/**/}
{/* }> */}
{/* }> */}
{/* }>*/}
{/* */}
{element}
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {BrowserRouter} from "react-router-dom";
import Demo from "./components/Demo";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
,
);
App.js
import React from 'react';
import {NavLink, useInRouterContext, useRoutes} from "react-router-dom";
import routes from "./routes";
import Header from "./components/Header";
function App(props) {
const element = useRoutes(routes)
console.log('@',useInRouterContext())
return (
About
Home
{/**/}
{/* }> */}
{/* }> */}
{/* }>*/}
{/* */}
{element}
);
}
export default App;
Demo.jsx
import React from 'react';
import {useInRouterContext} from "react-router-dom";
function Demo(props) {
console.log(useInRouterContext())
return (
demo
);
}
export default Demo;
Header.jsx
import React from 'react';
import {useInRouterContext, useNavigate} from "react-router-dom";
function Header(props) {
const navigate = useNavigate()
console.log(useInRouterContext())
function back() {
navigate(-1)
}
function forward() {
navigate(1)
}
return (
React Router Demo
);
}
export default Header;
News.jsx
import React from 'react';
import {useNavigationType} from "react-router-dom";
function News(props) {
console.log(useNavigationType())
return (
- news001
- news002
- news003
);
}
export default News;
const result = useoutlet( )
console.log( result)
//如果嵌套路由没有挂载,则result为null
//如果嵌套路由已经挂载,则展示嵌套的路由对象
Home.jsx
import React from 'react';
import {NavLink, Outlet, useOutlet} from "react-router-dom";
function Home(props) {
console.log(useOutlet())
return (
Home组件内容
-
News
-
Message
);
}
export default Home;
News.jsx
import React from 'react';
import {useResolvedPath} from "react-router-dom";
function News(props) {
console.log(useResolvedPath('/user?id=001&name=tom#qwe'))
return (
- news001
- news002
- news003
);
}
export default News;
1.
import React from "react";
import ReactDOM from "react-dom" ;
import { BrowserRouter } from "react-router-dom" ;
ReactDOM.render(
{/*整体结构(通常为App组件)*/}
,root
);
2.
3.
4.
5.
6.
7.
1.useRoutes()
2.useNavigate
3. useParams()
4.useSearchParams()
5.useLocation()
6.useMatch()
https://gitee.com/coderPatrickStar/react/tree/master/20231003