goBack()带我到第一个入口,而不是我想去的地方

Kar*_*ran 8 browser-history reactjs react-router isomorphic-javascript

我正在创建一个应用程序,我登录/注销有一个家,关于,配置文件等.它使用反应路由器,但是当我登录时,我希望应用程序转换到它所在的最后一条路线,它有时适用于例如,如果我访问,然后登录并登录,它会带我回到约,但如果我访问应用程序根目录'/'然后去登录,并登录,它会带我回到我曾经在的第一页(当您打开浏览器或选项卡时,这将是默认页面).我这样写了,我觉得我这样做的方式非常幼稚.我在渲染方法中做到了,它被称为很多.我认为它有时会这样做,但我不完全确定.希望得到一些建议,代码:

constructor(props, context) {
    super(props, context);
    this.state = UserStore.getState();
    this.context = context;
  }

render() {

    console.log(this.state.user.get('authenticated'));
    if(this.state.user.get('authenticated')){
        this.context.history.goBack();
    }

     return (
    //stuff
    )
Run Code Online (Sandbox Code Playgroud)

我是否应该在我访问的每条路线上将状态推入历史记录中,我还注意到,如果我在登录之前等待一段时间,有时它会正确执行,也许它会将其添加到历史记录中稍晚或需要一些时间?不完全确定如何调试它,console.log(this.context.history)只是显示了它的一些功能,我想我可能会在错误的地方看?

路线:

export default (
  <Route component={App}>
    <Route path="/" component={Dashboard} />
    <Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
    <Route path="about" component={About} />
    <Route path="profile" component={Profile} onEnter={requireAuth}/>
    <Route path="login" component={LoginSignupPage} />
    <Route path="channels" component={Channels} />
  </Route>
);
Run Code Online (Sandbox Code Playgroud)

注入反应路由器

import React from 'react';
import ReactDOM from 'react-dom';
import Iso from 'iso';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import { Router } from 'react-router';

import alt from 'altInstance';
import routes from 'routes.jsx';
import injectTapEventPlugin from 'react-tap-event-plugin';
window.React = React;
injectTapEventPlugin();

/*
 * Client side bootstrap with iso and alt
 */
Iso.bootstrap((state, _, container) => {
  alt.bootstrap(state);
  ReactDOM.render(<Router history={createBrowserHistory()} children={routes} />, container);
});
Run Code Online (Sandbox Code Playgroud)

Zha*_*hao 1

<Route path="/" component={App}>
    <IndexRedirect to='dashboard' />
    <Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
    <Route path="about" component={About} />
    <Route path="profile" component={Profile} onEnter={requireAuth}/>
    <Route path="login" component={LoginSignupPage} />
    <Route path="channels" component={Channels} />
</Route>
Run Code Online (Sandbox Code Playgroud)

像这样更改路线可能会解决您的问题。路径“/”应该是“login”的父级别路由,但不是同一级别