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

后端 未结 11 2140
旧时难觅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:48

    This should be done in the component that is being routed to. In this case, it is App component. Therefore, in App.js, import "createBrowserHistory" and do it as follows:

    import React, { Component } from 'react';
    import './App.css';
    import { createBrowserHistory } from "history";
    
    class App extends Component {
        constructor(props){
            super(props);
    
            this.history = createBrowserHistory();;
    
            this.state = {
                headerText: "Props from Header.",
                contentText: "Props from content."
            };
        }
        render() {
            return (
              
            );
        }
    }
    
    export default App;
    

提交回复
热议问题