首页 > 解决方案 > How to preserve last page when using back button with react-router?

问题描述

I’m doing a redirect from within a component’s render like this:

import React, { Component } from "react";
import browserHistory from "react-router/lib/browserHistory";

class LoginRedirectExample extends Component {
  constructor(props) {
    super(props)
  }

  redirectToLogin() {
    browserHistory.push("/login”);
    return null;
  }

  render () {
    return (
       <div className={c("container")}>
        {
          this.props.isLoggedIn ? this.redirectToLogin() : <button>Click Me</button>
        }
       </div>
    )
  }
}

Works great - but when if I’m redirected to the login view - I can’t use the browser back button - it doesn’t do anything. How do I handle this redirect and still have the functionality of the back button with react router? I’m using react-router 3 if that makes a difference.

标签: javascriptreactjsreact-router

解决方案


推荐阅读