react-router go back a page how do you configure history?

前端 未结 21 1156
说谎
说谎 2020-11-30 18:30

Can anyone please tell me how I can go back to the previous page rather than a specific route?

When using this code:

var BackButton = React.createCla         


        
相关标签:
21条回答
  • 2020-11-30 18:45
    import { withRouter } from 'react-router-dom'
    
    this.props.history.goBack();
    

    I am using these versions

    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    
    0 讨论(0)
  • 2020-11-30 18:47

    According to https://reacttraining.com/react-router/web/api/history

    For "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,
    };
    
    0 讨论(0)
  • 2020-11-30 18:48

    Update with React v16 and ReactRouter v4.2.0 (October 2017):

    class BackButton extends Component {
      static contextTypes = {
        router: () => true, // replace with PropTypes.object if you use them
      }
    
      render() {
        return (
          <button
            className="button icon-left"
            onClick={this.context.router.history.goBack}>
              Back
          </button>
        )
      }
    }
    

    Update with React v15 and ReactRouter v3.0.0 (August 2016):

    var browserHistory = ReactRouter.browserHistory;
    
    var BackButton = React.createClass({
      render: function() {
        return (
          <button
            className="button icon-left"
            onClick={browserHistory.goBack}>
            Back
          </button>
        );
      }
    });
    

    Created a fiddle with a little bit more complex example with an embedded iframe: https://jsfiddle.net/kwg1da3a/

    React v14 and ReacRouter v1.0.0 (Sep 10, 2015)

    You can do this:

    var React = require("react");
    var Router = require("react-router");
    
    var SomePage = React.createClass({
      ...
    
      contextTypes: {
        router: React.PropTypes.func
      },
      ...
    
      handleClose: function () {
        if (Router.History.length > 1) {
          // this will take you back if there is history
          Router.History.back();
        } else {
          // this will take you to the parent route if there is no history,
          // but unfortunately also add it as a new route
          var currentRoutes = this.context.router.getCurrentRoutes();
          var routeName = currentRoutes[currentRoutes.length - 2].name;
          this.context.router.transitionTo(routeName);
        }
      },
      ...
    

    You need to be careful that you have the necessary history to go back. If you hit the page directly and then hit back it will take you back in the browser history before your app.

    This solution will take care of both scenarios. It will, however, not handle an iframe that can navigate within the page (and add to the browser history), with the back button. Frankly, I think that is a bug in the react-router. Issue created here: https://github.com/rackt/react-router/issues/1874

    0 讨论(0)
  • 2020-11-30 18:51

    Call the following component like so:

    <BackButton history={this.props.history} />
    

    And here is the component:

    import React, { Component } from 'react'
    import PropTypes from 'prop-types'
    class BackButton extends Component {
      constructor() {
        super(...arguments)
    
        this.goBack = this.goBack.bind(this)
      }
    
      render() {
        return (
          <button
            onClick={this.goBack}>
              Back
          </button>
        )
      }
    
      goBack() {
        this.props.history.goBack()
      }
    }
    
    BackButton.propTypes = {
      history: PropTypes.object,
    }
    
    export default BackButton
    

    I'm using:

    "react": "15.6.1"
    "react-router": "4.2.0"
    
    0 讨论(0)
  • 2020-11-30 18:52

    REDUX

    You can also use react-router-redux which has goBack() and push().

    Here is a sampler pack for that:

    In your app's entry point, you need ConnectedRouter, and a sometimes tricky connection to hook up is the history object. The Redux middleware listens to history changes:

    import React from 'react'
    import { render } from 'react-dom'
    import { ApolloProvider } from 'react-apollo'
    import { Provider } from 'react-redux'
    import { ConnectedRouter } from 'react-router-redux'
    import client from './components/apolloClient'
    import store, { history } from './store'
    import Routes from './Routes'
    import './index.css'
    
    render(
      <ApolloProvider client={client}>
        <Provider store={store}>
          <ConnectedRouter history={history}>
            <Routes />
          </ConnectedRouter>
        </Provider>
      </ApolloProvider>,
      document.getElementById('root'),
    )
    

    I will show you a way to hook up the history. Notice how the history is imported into the store and also exported as a singleton so it can be used in the app's entry point:

    import { createStore, applyMiddleware, compose } from 'redux'
    import { routerMiddleware } from 'react-router-redux'
    import thunk from 'redux-thunk'
    import createHistory from 'history/createBrowserHistory'
    import rootReducer from './reducers'
    
    export const history = createHistory()
    
    const initialState = {}
    const enhancers = []
    const middleware = [thunk, routerMiddleware(history)]
    
    if (process.env.NODE_ENV === 'development') {
      const { devToolsExtension } = window
      if (typeof devToolsExtension === 'function') {
        enhancers.push(devToolsExtension())
      }
    }
    
    const composedEnhancers = compose(applyMiddleware(...middleware), ...enhancers)
    const store = createStore(rootReducer, initialState, composedEnhancers)
    
    export default store
    

    The above example block shows how to load the react-router-redux middleware helpers which complete the setup process.

    I think this next part is completely extra, but I will include it just in case someone in the future finds benefit:

    import { combineReducers } from 'redux'
    import { routerReducer as routing } from 'react-router-redux'
    
    export default combineReducers({
      routing, form,
    })
    

    I use routerReducer all the time because it allows me to force reload Components that normally do not due to shouldComponentUpdate. The obvious example is when you have a Nav Bar that is supposed to update when a user presses a NavLink button. If you go down that road, you will learn that Redux's connect method uses shouldComponentUpdate. With routerReducer, you can use mapStateToProps to map routing changes into the Nav Bar, and this will trigger it to update when the history object changes.

    Like this:

    const mapStateToProps = ({ routing }) => ({ routing })
    
    export default connect(mapStateToProps)(Nav)
    

    Forgive me while I add some extra keywords for people: if your component isn't updating properly, investigate shouldComponentUpdate by removing the connect function and see if it fixes the problem. If so, pull in the routerReducer and the component will update properly when the URL changes.

    In closing, after doing all that, you can call goBack() or push() anytime you want!

    Try it now in some random component:

    1. Import in connect()
    2. You don't even need mapStateToProps or mapDispatchToProps
    3. Import in goBack and push from react-router-redux
    4. Call this.props.dispatch(goBack())
    5. Call this.props.dispatch(push('/sandwich'))
    6. Experience positive emotion

    If you need more sampling, check out: https://www.npmjs.com/package/react-router-redux

    0 讨论(0)
  • 2020-11-30 18:54

    For react-router v2.x this has changed. Here's what I'm doing for ES6:

    import React from 'react';
    import FontAwesome from 'react-fontawesome';
    import { Router, RouterContext, Link, browserHistory } from 'react-router';
    
    export default class Header extends React.Component {
    
      render() {
        return (
          <div id="header">
            <div className="header-left">
              {
                this.props.hasBackButton &&
                <FontAwesome name="angle-left" className="back-button" onClick={this.context.router.goBack} />
              }
            </div>
            <div>{this.props.title}</div>
          </div>
        )
      }
    }
    
    Header.contextTypes = {
      router: React.PropTypes.object
    };
    
    Header.defaultProps = {
      hasBackButton: true
    };
    
    Header.propTypes = {
      title: React.PropTypes.string
    };
    
    0 讨论(0)
提交回复
热议问题