React-router TypeError: _this.props.history is undefined

后端 未结 11 2168
旧时难觅i
旧时难觅i 2020-12-15 16:07

I am using react-router with react js and i following their documentation but facing this error

while compiling it shows the error,

TypeError: _this.         


        
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 17:10

    I encountered this problem when trying to instantiate my own Router rather than using one of the built-in ones, to deal with an issue of having access to the history:

    history.js

    import { createHashHistory as createHistory } from 'history'
    
    export default createHistory()
    

    root.jsx

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

    In my case, the trouble was that I had a version of the history package locked at v2.1.2, while react-router was depending on a v4.x version of history in its own dependencies. So the built-in routers instantiated fine using the newer history, but when I tried to do it myself I was pulling in the older history and getting an incompatible object.

提交回复
热议问题