react router v^4.0.0 Uncaught TypeError: Cannot read property 'location' of undefined

前端 未结 4 1589
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 15:57

I\'ve been having some trouble with react router (i\'m using version^4.0.0).

this is my index.js

import React from \'react\';
import ReactDOM from \'         


        
相关标签:
4条回答
  • 2020-12-12 16:17

    You're doing a few things wrong.

    1. First, browserHistory isn't a thing in V4, so you can remove that.

    2. Second, you're importing everything from react-router, it should be react-router-dom.

    3. Third, react-router-dom doesn't export a Router, instead, it exports a BrowserRouter so you need to import { BrowserRouter as Router } from 'react-router-dom.

    Looks like you just took your V3 app and expected it to work with v4, which isn't a great idea.

    0 讨论(0)
  • 2020-12-12 16:17

    Replace

    import { Router, Route, Link, browserHistory } from 'react-router';
    

    With

    import { BrowserRouter as Router, Route } from 'react-router-dom';
    

    It will start working. It is because react-router-dom exports BrowserRouter

    0 讨论(0)
  • 2020-12-12 16:32

    so if you need want use this code )

    `import { useRoutes } from "./routes";
    
    import { BrowserRouter as Router } from react-router-dom";
    
    export const App = () => {`
    
    const routes = useRoutes(true);`
    
      return (
    
        <Router>
    
          <div className="container">{routes}</div>
    
        </Router>
    
      );
    
    };
    

    // ----

    import { Switch, Route, Redirect } from "react-router-dom";
    
    export const useRoutes = (isAuthenticated) => {
      if (isAuthenticated) {
        return (
          <Switch>
            <Route path="/links" exact>
              <LinksPage />
            </Route>
            <Route path="/create" exact>
              <CreatePage />
            </Route>
            <Route path="/detail/:id">
              <DetailPage />
            </Route>
            <Redirect path="/create" />
          </Switch>
        );
      }
      return (
        <Switch>
          <Route path={"/"} exact>
            <AuthPage />
          </Route>
          <Redirect path={"/"} />
        </Switch>
      );
    };
    
    0 讨论(0)
  • 2020-12-12 16:43

    I've tried everything suggested here but didn't work for me. So in case I can help anyone with a similar issue, every single tutorial I've checked is not updated to work with version 4.

    Here is what I've done to make it work

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

    That's the only way I have managed to make it work without any errors or warnings.

    In case you want to pass props to your component for me the easiest way is this one:

     <Route path="/" render={()=><App items={temasArray}/>}/>
    
    0 讨论(0)
提交回复
热议问题