The prop `history` is marked as required in `Router`, but its value is `undefined`. in Router

前端 未结 7 863
甜味超标
甜味超标 2021-01-30 01:50

I am new to ReactJs. This is my code:

var React = require(\'react\');
var ReactDOM = require(\'react-dom\');
var {Route, Router, IndexRoute, hashHistory} = requ         


        
相关标签:
7条回答
  • 2021-01-30 02:26

    The below works for me with "react-router@^3.0.5":

    package.json:

    "react-dom": "^16.6.0",
    "react-router": "^3.0.5"
    

    index.js:

    import { render } from 'react-dom'
    import { App } from './components/App'
    import { NotFound404 } from './components/NotFound404'
    import { Router, Route, hashHistory } from 'react-router'
    
    render(
        <Router history={hashHistory}>
            <Route path='/' component={App} />
            <Route path='*' component={NotFound404} />
        </Router>,
        document.getElementById('root')
    )
    
    0 讨论(0)
  • 2021-01-30 02:27

    Which version of React Router are you using? Router version 4 changed from passing in the browserHistory class to passing an instance of browserHistory, see the code example in the new docs.

    This has been catching lots people who automatically upgrade; a migration document will be out 'any day now'.

    You want to add this to the top:

    import { createBrowserHistory } from 'history'
    
    const newHistory = createBrowserHistory();
    

    and

    <Router history={newHistory}/>
    
    0 讨论(0)
  • 2021-01-30 02:34

    I also write a Login practice. And also meet the same question like you. After a day struggle, I found that only this.props.history.push('/list/') can make it instead of pulling in a lot of plugins. By the way, the react-router-dom version is ^4.2.2. Thanks!

    handleSubmit(e){
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err,values)=>{
            if(!err){
                this.setState({
                    visible:false
                });
                this.props.form.resetFields();
                console.log(values.username);
                const path = '/list/';
                this.props.history.push(path);
            }
        })
    }

    0 讨论(0)
  • 2021-01-30 02:36

    If you want to have multiple routes you can use switch like this,

    import {Switch} from 'react-router'; 
    

    then

    <BrowserRouter>
         <Switch>
             <Route exact path="/" component={TodoComponent} />
             <Route path="/about" component={About} />
         </Switch>
    </BrowserRouter>
    
    0 讨论(0)
  • 2021-01-30 02:42

    If you are using react-router v4 you need to install react-router-dom as well. After that, import BrowserRouter from react-router-dom and switch Router for BrowserRouter. It seems that v4 change several things. Also, the react-router documentation is outdated. This is my working code:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { BrowserRouter, Route } from 'react-router-dom'
    import App from './components/App';
    
    ReactDOM.render((
         <BrowserRouter>
              <Route path="/" component={App}/>
         </BrowserRouter>
         ),
         document.getElementById('root')
    );
    

    Source

    0 讨论(0)
  • 2021-01-30 02:50

    I got the same problem in ES6, but when I switched to use 'react-router-dom' library, the problem was solved. For all fans of ES6, here we go:

    npm install --save react-router-dom

    In index.js:

    import {HashRouter, Route} from 'react-router-dom';
    import App from './App';
    
    ReactDOM.render(
        <HashRouter>
            <Route path="/" component={App}/>
        </HashRouter>
      ,
      document.getElementById('root')
    );
    
    0 讨论(0)
提交回复
热议问题