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

后端 未结 11 2049
旧时难觅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 16:49

    This is because things changed in React Router starting with 4.0.0. You should now use BrowserRouter from react-router-dom package when you were using browserHistory. So your code will looks like:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    import './index.css';
    import { BrowserRouter, Route } from 'react-router-dom';
    
    ReactDOM.render(
        
            
        , document.getElementById('root')
    );
    

    Of course, you'll need to install react-router-dom first.

    Also note that if you're using more than one Route element, you'll have to use a Switch as explained in this answer.

提交回复
热议问题