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

当使用此代码时:

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.context.router.history.goBack()

其他回答

对我有用的是在我的文件顶部导入使用throuter;

import { withRouter } from 'react-router-dom'

然后用它来包装导出的函数在我的文件底部;

export default withRouter(WebSitePageTitleComponent)

然后允许我访问路由器的历史道具。完整的示例代码如下!

import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'

import PropTypes from 'prop-types'

class TestComponent extends Component {
  constructor(props) {
    super(props)
    this.handleClick = this.handleClick.bind(this)
  }

  handleClick() {
    event.preventDefault()
    this.props.history.goBack()
  }

  render() {
    return (
      <div className="page-title">
        <a className="container" href="/location" onClick={this.handleClick}>
          <h1 className="page-header">
            { this.props.title }
          </h1>
        </a>
      </div>
    )
  }
}

const { string, object } = PropTypes

TestComponent.propTypes = {
  title: string.isRequired,
  history: object
}

export default withRouter(TestComponent)

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>
)

我认为你只需要在路由器上启用BrowserHistory,初始化如下:< router history={new BrowserHistory}>。

在此之前,你应该从'react-router/lib/BrowserHistory'中获取BrowserHistory

下面是一个使用ES6的例子

const BrowserHistory = require('react-router/lib/BrowserHistory').default;

const App = React.createClass({
    render: () => {
        return (
            <div><button onClick={BrowserHistory.goBack}>Go Back</button></div>
        );
    }
});

React.render((
    <Router history={BrowserHistory}>
        <Route path="/" component={App} />
    </Router>
), document.body);

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

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

使用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>