简体   繁体   English

React - history.push 重定向不起作用

[英]React - history.push redirect not working

I'm trying to make my page redirect after login by using .then type ajax.我正在尝试使用.then使我的页面在登录后重定向,然后键入 ajax。 Everything's working except the history.push() command:除了history.push()命令外,一切正常:

axios
  .post('http://localhost:8080/login', {
    email,
    password
  })
  .then((res) => {
    sessionStorage.token = res.data.token;
    const { sub } = JSON.parse(atob(res.data.token.split('.')[1]));
    sessionStorage.email = sub;
  })
  .then(() => history.push("/reservations"))
  .catch(() => setError(connectionError))
}

Instead of redirecting, it's logging in correctly but not changing the page.它不是重定向,而是正确登录但不更改页面。 The page updates when I refresh, but still no redirect.刷新时页面会更新,但仍然没有重定向。 Not sure if it matters, but here's my routing:不确定是否重要,但这是我的路线:

<Route path="/reservations" exact component={Reservations}/>

Help much appreciated非常感谢帮助

There's something wrong with the syntax of the last .then .最后一个.then的语法有问题。 Shouldn't it be a callback method inside which you'll call history.push ?它不应该是一个回调方法,您将在其中调用history.push吗?

Something like this:像这样的东西:

axios
  .post('http://localhost:8080/login', {
    email,
    password
  })
  .then((res) => {
    sessionStorage.token = res.data.token;
    const { sub } = JSON.parse(atob(res.data.token.split('.')[1]));
    sessionStorage.email = sub;
  })
  .then(() => history.push("/reservations"))
  .catch(() => setError(connectionError))
}

I will add the answer from SiddAjmera, if history still doesn't work, you have to add a package history我将添加来自 SiddAjmera 的答案,如果历史仍然不起作用,则必须添加 package 历史

npm install history npm 安装历史

index.js index.js

import React from "react";
import ReactDOM from "react-dom";
import { Router } from "react-router";
import { createBrowserHistory } from "history";

export const history = createBrowserHistory();

ReactDOM.render(
  <Router history={history}>
    <App />
  </Router>,
  node
);
import {history} from "./index"
axios
  .post('http://localhost:8080/login', {
    email,
    password
  })
  .then((res) => {
    sessionStorage.token = res.data.token;
    const { sub } = JSON.parse(atob(res.data.token.split('.')[1]));
    sessionStorage.email = sub;
  })
  .then(() => history.push("/reservations"))
  .catch(() => setError(connectionError))
}

Try it this way:试试这种方式:

axios
      .post('http://localhost:8080/login', {
        email,
        password
      })
      .then((res) => {
        sessionStorage.token = res.data.token;
        const { sub } = JSON.parse(atob(res.data.token.split('.')[1]));
        sessionStorage.email = sub;
        history.push("/reservations");
      })
      .catch(() => setError(connectionError))
    }

Change .then(history.push("/reservations")) to .then(() => history.push("/reservations")) ..then(history.push("/reservations"))更改为.then(() => history.push("/reservations"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM