Programmatically navigate using react router

后端 未结 30 2314
無奈伤痛
無奈伤痛 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:27

    Those who are facing issues in implementing this on react-router v4.

    Here is a working solution for navigating through the react app from redux actions.

    history.js

    import createHistory from 'history/createBrowserHistory'
    
    export default createHistory()
    

    App.js/Route.jsx

    import { Router, Route } from 'react-router-dom'
    import history from './history'
    ...
    
     
    
    

    another_file.js OR redux file

    import history from './history' 
    
    history.push('/test') // this should change the url and re-render Test component
    

    All thanks to this comment: ReactTraining issues comment

提交回复
热议问题