react-router-dom6 实现编程式跳转路由


概述:通过 useNavigate() hook 实现

import {
    BrowserRouter,
    Routes,
    Route,
    useNavigate,
} from "react-router-dom";
 
 
export default function App(){
    return(
        <BrowserRouter>
            <Routes>
                <Route path="/" element={<Index />}></Route>
                <Route path="/home" element={<Home />}></Route>
            </Routes>
        </BrowserRouter>
    )
}
 
 
function Index(){
    let navigate = useNavigate()
    return(
        <button onClick={() => navigate("/home")}>to: home</button>
    )
}
function Home(){
    let navigate = useNavigate()
    return(
        <button onClick={() => navigate("/")}>to: index</button>
    )
}

声明:小小博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - react-router-dom6 实现编程式跳转路由


Carpe Diem and Do what I like