有没有人能告诉我如何回到上一页,而不是具体的路线?

当使用此代码时:

var BackButton = React.createClass({

 mixins: [Router.Navigation],
  render: function() {
    return (
        <button
            className="button icon-left"
            onClick={this.navigateBack}>
            Back
        </button>
    );
  },

  navigateBack: function(){
    this.goBack();
  }
});

得到这个错误,goBack()被忽略,因为没有路由器历史

以下是我的路线:

// Routing Components
Route = Router.Route;
RouteHandler = Router.RouteHandler;
DefaultRoute = Router.DefaultRoute;

var routes = (
 <Route name="app" path="/" handler={OurSchoolsApp}>
     <DefaultRoute name="home" handler={HomePage} />
     <Route name="add-school" handler={AddSchoolPage}  />
     <Route name="calendar" handler={CalendarPage}  />
     <Route name="calendar-detail" path="calendar-detail/:id" handler={CalendarDetailPage} />
     <Route name="info-detail" path="info-detail/:id" handler={InfoDetailPage} />
     <Route name="info" handler={InfoPage} />
     <Route name="news" handler={NewsListPage} />
     <Route name="news-detail" path="news-detail/:id" handler={NewsDetailPage} />
     <Route name="contacts" handler={ContactPage} />
     <Route name="contact-detail" handler={ContactDetailPage} />
     <Route name="settings" handler={SettingsPage} />
 </Route>
 );

 Router.run(routes, function(Handler){
   var mountNode = document.getElementById('app');
   React.render(<Handler /> , mountNode);
 });

当前回答

这适用于浏览器和哈希历史。

this.props.history.goBack();

其他回答

ES6方法无mixins使用react-router,无状态函数。

import React from 'react'
import { browserHistory } from 'react-router'

export const Test = () => (
  <div className="">
    <button onClick={browserHistory.goBack}>Back</button>
  </div>
)

使用React钩子

进口:

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

在无状态组件中:

let history = useHistory();

呼叫事件:

history.goBack()

在event Button中使用的例子:

<button onClick={history.goBack}>Back</button>

or

<button onClick={() => history.goBack()}>Back</button>

这适用于浏览器和哈希历史。

this.props.history.goBack();

根据https://reacttraining.com/react-router/web/api/history

对于“react-router-dom”:“^5.1.2”,,

const { history } = this.props;
<Button onClick={history.goBack}>
  Back
</Button>
YourComponent.propTypes = {
  history: PropTypes.shape({
    goBack: PropTypes.func.isRequired,
  }).isRequired,
};

进口withRouter import {withRouter} from 'react-router-dom'; 导出你的组件为: 导出withRouter (nameofcomponent) 例如,在按钮单击时,调用goBack: 回<按钮onClick = {this.props.history.goBack} > < / >按钮

在react-router-dom v4.3上测试