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

前端 未结 21 1301
说谎
说谎 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:42

    In react-router v4.x you can use history.goBack which is equivalent to history.go(-1).

    App.js

    import React from "react";
    import { render } from "react-dom";
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    import Home from "./Home";
    import About from "./About";
    import Contact from "./Contact";
    import Back from "./Back";
    
    const styles = {
      fontFamily: "sans-serif",
      textAlign: "left"
    };
    
    const App = () => (
      
    • Home
    • About
    • Contact

    {/* <----- This is component that will render Back button */}
    ); render(, document.getElementById("root"));

    Back.js

    import React from "react";
    import { withRouter } from "react-router-dom";
    
    const Back = ({ history }) => (
      
    );
    
    export default withRouter(Back);
    

    Demo: https://codesandbox.io/s/ywmvp95wpj

    Please remember that by using history your users can leave because history.goBack() can load a page that visitor has visited before opening your application.


    To prevent such situation as described above, I've created a simple library react-router-last-location that watch your users last location.

    Usage is very straight forward. First you need to install react-router-dom and react-router-last-location from npm.

    npm install react-router-dom react-router-last-location --save
    

    Then use LastLocationProvider as below:

    App.js

    import React from "react";
    import { render } from "react-dom";
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    import { LastLocationProvider } from "react-router-last-location";
    //              ↑
    //              |
    //              |
    //
    //       Import provider
    //
    import Home from "./Home";
    import About from "./About";
    import Contact from "./Contact";
    import Back from "./Back";
    
    const styles = {
      fontFamily: "sans-serif",
      textAlign: "left"
    };
    
    const App = () => (
      
    Click on About to see your last location
    {/* <---- Put provider inside */}
    • Home
    • About
    • Contact

    ); render(, document.getElementById("root"));

    Back.js

    import React from "react";
    import { Link } from "react-router-dom";
    import { withLastLocation } from "react-router-last-location";
    //              ↑
    //              |
    //              |
    //
    //    `withLastLocation` higher order component
    //    will pass `lastLocation` to your component               
    //
    //                   |
    //                   |
    //                   ↓
    const Back = ({ lastLocation }) => (
      lastLocation && Back to previous page
    );
    
    
    //          Remember to wrap
    //   your component before exporting
    //
    //                   |
    //                   |
    //                   ↓
    export default withLastLocation(Back);
    

    Demo: https://codesandbox.io/s/727nqm99jj

提交回复
热议问题