最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Router history.goBack doesn't work when using <Redirect > - Stack Overflow

programmeradmin1浏览0评论

In a React App, I use Router like this:

let path='/';
if(condition){
   path='/dashboard'
}else if(condition){
   path='/login'
}

<Router>
 <Redirect to={path} />
   <Switch>
      <Route path="/dashboard"><Dashboard /></Route>
      <Route path="/login"><Login /></Route>
    </Switch>
</Router>

This works fine. But I want to implement a Back Button in each page to move along pages. I do this:

// The login.jsx ponent
function Post(props) {
  return (
    <div>
      <button type="button" onClick={() => props.history.goBack()}>
        Go back
      </button>
    </div>
  );
}

But I get this error:

Uncaught TypeError: Cannot read property 'goBack' of undefined

Then I follow another approach. I use useHistory:

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Redirect,
  Link,
  useHistory,
} from "react-router-dom";

// The login.jsx ponent
function Post(props) {
  let history = useHistory();
  
  return (
    <div>
      <button type="button" onClick={() => history.goBack()}>
        Go back
      </button>
    </div>
  );
}

And this time when I click on the Go Back button, nothing happens!

If I do this procedures with <Link/> it works fine. But when I use <Redirect /> approach, it doesn't work. is there any alternative to my redirect approach? or is there any solution to current approach? thanks.

In a React App, I use Router like this:

let path='/';
if(condition){
   path='/dashboard'
}else if(condition){
   path='/login'
}

<Router>
 <Redirect to={path} />
   <Switch>
      <Route path="/dashboard"><Dashboard /></Route>
      <Route path="/login"><Login /></Route>
    </Switch>
</Router>

This works fine. But I want to implement a Back Button in each page to move along pages. I do this:

// The login.jsx ponent
function Post(props) {
  return (
    <div>
      <button type="button" onClick={() => props.history.goBack()}>
        Go back
      </button>
    </div>
  );
}

But I get this error:

Uncaught TypeError: Cannot read property 'goBack' of undefined

Then I follow another approach. I use useHistory:

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Redirect,
  Link,
  useHistory,
} from "react-router-dom";

// The login.jsx ponent
function Post(props) {
  let history = useHistory();
  
  return (
    <div>
      <button type="button" onClick={() => history.goBack()}>
        Go back
      </button>
    </div>
  );
}

And this time when I click on the Go Back button, nothing happens!

If I do this procedures with <Link/> it works fine. But when I use <Redirect /> approach, it doesn't work. is there any alternative to my redirect approach? or is there any solution to current approach? thanks.

Share asked Jul 12, 2020 at 12:35 FcoderFcoder 9,22618 gold badges68 silver badges103 bronze badges 1
  • 1 The way of using useHistory() is correct but the problem is you have to fill the history stack in order for goBack() to work. If there is nothing in the stack, goBack() goes nowhere. So you have to first push your redirects to Router history. – wr93_ Commented Jul 12, 2020 at 13:14
Add a ment  | 

1 Answer 1

Reset to default 5

For redirects to be included in the history, use push

<Redirect push to={path} />

More information here from the docs

发布评论

评论列表(0)

  1. 暂无评论