TypeError:无法读取属性' history'未定义的

时间:2017-08-25 22:48:21

标签: javascript reactjs react-router react-router-v4 react-router-dom

我在尝试将/ logout路由命中时将用户重定向到登录页面。 auth正在运行(jwt令牌被删除,但应用程序无法重定向到/ login。

此外,如果我这样做,应用程序也会崩溃。

在登录路由中,我使用了来自react-router-dom包的路由器,我可以访问this.props.history.push(' / redirect_to_a_new_path'),但当我尝试打包时使用withRouter方法的应用程序组件也会崩溃。

请帮忙!

以下是github repo

App.js

import React, { Component } from "react";
import {
  BrowserRouter as Router,
  Route,
  Switch,
  withRouter
} from "react-router-dom";
import MuiThemeProvider from "material-ui/styles/MuiThemeProvider";
import lightBaseTheme from "material-ui/styles/baseThemes/lightBaseTheme";
import getMuiTheme from "material-ui/styles/getMuiTheme";
import injectTapEventPlugin from "react-tap-event-plugin";

// Components
import Navbar from "./components/Navbar";
import HomePage from "./components/HomePage";
import SpotMap from "./components/SpotMap";
import SignUpPage from "./components/containers/SignUpPage";
import LoginPage from "./components/containers/LoginPage";
import DashboardPage from "./components/containers/DashBoardPage";
import NotFound from "./components/NoteFound";
import Auth from "./modules/Auth";
import "./styles/App.css";

injectTapEventPlugin();

const handleLogout = event => {
  Auth.deauthenticateUser();
  this.props.history.push("/login");
};

const isLoggedIn = event => {
  if (Auth.isUserAuthenticated()) {
    this.props.history.push(DashboardPage);
  } else {
    this.props.history.push(HomePage);
  }
};

class App extends Component {
  render() {
    return (
      <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
        <Router>
          <div>
            <Navbar />
            <Switch>
              <Route exact path="/" component={isLoggedIn} />
              <Route path="/spotmap" component={SpotMap} />
              <Route path="/dashboard" component={DashboardPage} />
              <Route path="/signup" component={SignUpPage} />
              <Route path="/login" component={LoginPage} />
              <Route path="/logout" component={handleLogout} />
              <Route component={NotFound} />
            </Switch>
          </div>
        </Router>
      </MuiThemeProvider>
    );
  }
}

export default App;

这是在React Router v3中编写的,这是我需要转换为React Router V4的内容。 对我不起作用的路线是&#34; /&#34;和&#34;退出&#34;路由。

import Base from './components/Base.jsx';
import HomePage from './components/HomePage.jsx';
import DashboardPage from './containers/DashboardPage.jsx';
import LoginPage from './containers/LoginPage.jsx';
import SignUpPage from './containers/SignUpPage.jsx';
import Auth from './modules/Auth';


const routes = {
  // base component (wrapper for the whole application).
  component: Base,
  childRoutes: [

    {
      path: '/',
      getComponent: (location, callback) => {
        if (Auth.isUserAuthenticated()) {
          callback(null, DashboardPage);
        } else {
          callback(null, HomePage);
        }
      }
    },

    {
      path: '/login',
      component: LoginPage
    },

    {
      path: '/signup',
      component: SignUpPage
    },

    {
      path: '/logout',
      onEnter: (nextState, replace) => {
        Auth.deauthenticateUser();

        // change the current URL to /
        replace('/');
      }
    }

  ]
};

export default routes;

2 个答案:

答案 0 :(得分:1)

对于/路线,您应该使用渲染,如下所示:

<Route exact path="/" render={() => {
  if (Auth.isUserAuthenticated()) { 
    (<DashboardPage)/>)
  } else {
    (<HomePage/>)
  }
}} />

使用/logout

<Redirect>路线
<Route path="/logout" render={() => {
    Auth.deauthenticateUser();
    return <Redirect to={{ pathname: "/login" }} />;
    }}
/>

答案 1 :(得分:0)

我使用 withRouter this.props.history.push(&#39; / login&#39;)发布/ logout路径的第二个解决方案。< / p>

  1. 将/ logout路由指向其自己的组件&#34; Logout&#34;
  2. 使用来自react-router-dom的withRouter方法包装Logout组件。
  3. <强> Logout.js

    import React, { Component } from "react";
    import Auth from "../modules/Auth";
    import { withRouter } from "react-router-dom";
    
    class Logout extends Component {
      constructor(props) {
        super(props);
        this.handleLogout = this.handleLogout.bind(this);
      }
      handleLogout() {
        Auth.deauthenticateUser();
        this.props.history.push("/login");
      }
      render() {
        return (
          <div>
            {this.handleLogout()}
          </div>
        );
      }
    }
    export default withRouter(Logout);