开发者问题收集

类型错误:无法读取未定义的属性“history”

2017-08-25
24189

当 /logout 路由被点击时,我尝试将用户重定向到登录页面。身份验证正在运行(jwt 令牌已被删除,但应用程序无法重定向到 /login。

此外,如果我对 / 执行此操作,应用程序也会崩溃。

在登录路由上,我使用 react-router-dom 包中的 withRouter,这样我可以访问 this.props.history.push('/redirect_to_a_new_path') ,但是当我尝试使用 withRouter 方法包装 App 组件时,它也会崩溃。

请帮忙!

这是 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 的内容路由器 V4。 对我来说不起作用的路由是“/”和“logout”路由。

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;
3个回答

对于 / 路线,您应该使用 render 如下:

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

对于您的 /logout 路线,使用 <Redirect>

<Route path="/logout" render={() => {
    Auth.deauthenticateUser();
    return <Redirect to={{ pathname: "/login" }} />;
    }}
/>
Edgar Henriquez
2017-08-26

我发布了使用 withRouter this.props.history.push('/login') 的 /logout 路径的第二个解决方案。

  1. 将 /logout 路由指向其自己的组件“Logout”
  2. 使用 react-router-dom 中的 withRouter 方法包​​装 Logout 组件。

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);
Isak La Fleur
2017-08-26

我会使用 useHistory。

请执行以下操作:

import { useHistory } from 'react-router-dom';

而不是:

import { useHistory } from 'react-router';

(否则会出现相同的错误消息。)

Ahmedakhtar11
2022-12-07