react-router-dom v6 programmatic navigation

When using withRouter for programmatic navigation, the terminal runs an error, indicating that v6 does not support withRouter for programmatic navigation, so how to perform programmatic navigation in v6?

The answer is to use useNavigate!

Examples are as follows:

import React from "react";
import {useNavigate} from "react-router-dom"
function Registe(){
    const navigate = useNavigate()
    function handleRegister(){
           navigate('/home')
    }
        
    return (
        <div>
            <button onClick={handleRegister}>跳转</button>
        </div>
    )
}
export default Registe
Note: useNavigate is only applicable to function components

Guess you like

Origin blog.csdn.net/sxp19980829/article/details/129688634