React router 4 history.listen never fires

后端 未结 2 1184
别那么骄傲
别那么骄傲 2021-01-01 18:19

Switched to router v4 and history v4.5.1 and now history listener not working

import createBrowserHistory from \'history/createBrowserHistory\'
const history         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-01 18:47

    Since you are using BrowserRouter(with import alias Router as mentioned in comments of the question), it doesn't care the history prop you pass in. Instead of that it internally creates and assigns new browser history to the Router. So the history instance that you listen and being used in Router is not the same. That's why your listener doesn't work.

    Import the original Router.

    import { Router } from 'react-router-dom';
    

    It will work as you expect.

提交回复
热议问题