Programmatically navigate using react router

后端 未结 30 2388
無奈伤痛
無奈伤痛 2020-11-21 05:18

With react-router I can use the Link element to create links which are natively handled by react router.

I see internally it calls t

30条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:36

    Try hookrouter instead, "the modern alternative to react-router"

    https://www.npmjs.com/package/hookrouter

    import { useRoutes, usePath, A} from "hookrouter";
    

    to answer OP question about linking through select box you can do it:

    navigate('/about');
    

    *** UPDATED ANSWER ***

    I think hook-router was a good starter kit and helped me learn about routing but have since updated to react-router for it's history and query parameter handling.

    import { useLocation, useHistory } from 'react-router-dom';
    
    
    const Component = (props) => {
        const history = useHistory();
        
        // Programmatically navigate
        history.push(newUrlString);
    }
    

    You push where you want to navigate into the location.history.

提交回复
热议问题