react router onEnter nextPathname 在做什么?

What is react router onEnter nextPathname doing?

在下面的代码中,如果不是 auth,则重定向到使用 replace pathname 登录,但是 state 在做什么?

已编辑 onEnter 不会将用户重定向到同一位置

login.js

    login() {

/*      ref.authWithOAuthPopup(this.props.provider, (error, authData) => {
            if (error) {
                console.log("Authentication Failed!", error);
            } else {
                console.log("Authenticated successfully with payload:", authData);
                console.log(this.state) // return null here
            }
        }) */
    }

authenticated.js

import Firebase from 'firebase';
import GLOBAL from './global.js';

var ref = new Firebase(GLOBAL.FIREBASE_URL);

export function requireAuth(nextState, replace) {

    ref.onAuth((authData) => {
        if ( !authData ) {
            replace ({
                pathname: '/login',
                state: { nextPathname: nextState.location.pathname }
            })
        }
    })
}

route.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, hashHistory, IndexRoute } from 'react-router';
import Layout from './pages/Layout';
import Purchased from './pages/Purchased';
import Home from './pages/Home';
import Profile from './pages/Profile';
import PurchasedItemDetail from './pages/PurchasedItemDetail';
import Login from './pages/Login';
import { requireAuth } from './utils/authenticated';

ReactDOM.render((
  <Router history={browserHistory}>
    <Route path="/" component={Layout}>
        <IndexRoute component={Home} onEnter={requireAuth} />
        <Route path="/login" component={Login} />
        <Route path="/purchased" component={Purchased} onEnter={requireAuth} />
        <Route path="/purchased/:purchasedItemID" component={PurchasedItemDetail} onEnter={requireAuth} />
        <Route path="/profile" component={Profile} onEnter={requireAuth} />
    </Route>
  </Router>
), document.getElementById('app')) 

此函数中的 nextState 参数是一个 Router State object,表示用户将要进入的应用程序的下一个状态。因此,当您传递 state: { nextPathname: nextState.location.pathname } 时,您是在告诉路由处理程序您希望将此数据一起发送。在这种情况下,它基本上是一个重定向路由。 Aka 登录后将他们重定向到某个位置。