React Router 6.x与React Router 5.x 版本相比,改变了什么?
-
内置组件: 移除Switch ,新增 Routes ;移除了 Redirect 组件,改用 Navigate 组件,新增Outlet等; -
语法:component={About} 变为 element={}等。 -
新增hook:useParams、useNavigate、useMatch等。 …
import './App.scss';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'
import Home from './pages/Home/Home';
import Setting from './pages/Setting/Setting';
function App() {
return (
<div className="App">
<Router>
<header>
<ul>
<Link to="/home"><li>首页</li></Link>
<Link to="/setting"><li>设置</li></Link>
</ul>
</header>
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="/setting" element={<Setting />}></Route>
<Route path="*" element={<Home />} />
</Routes>
</Router>
</div>
);
}
export default App;
参考链接: https://kalacloud.com/blog/react-router-tutorial/ https://reactrouter.com/docs/en/v6
|