反应路由器pushState不工作

时间:2016-04-28 07:54:32

标签: reactjs react-router

我有一个redux动作,如:

import createHistory from 'history/lib/createBrowserHistory'
import * as loginTypes from '../lib/constants/login'

export function login(){

    return (dispatch, getState) => {

        var message = "hellooww world"
        dispatch({ message, type: loginTypes.LOGIN })

        createHistory.push('/login')
        createHistory.pushState(null, '/login')
        createHistory.transitionTo('/login')

    }
}

在这里,我只是想在我的行动被调度之后重定向我,但它没有被重定向......这可能是什么问题?需要帮助

1 个答案:

答案 0 :(得分:0)

那里至少有两个问题。

1º)你不应该在减速器中推动状态。

2º)如果你想在不使用context.router的情况下推送状态,你必须使用从React路由器导入的browserHistory,更多信息here

E.G

import { browserHistory } from 'react-router'

  handleSubmit(event) {
    // ...
    const path = `/repos/${userName}/${repo}`
    browserHistory.push(path)
  },

此外,如果您打算使用browserHistory,请不要忘记将其用作路由器中的历史记录

import { Router, browserHistory } from 'react-router'
ReactDOM.render(
    <Provider store={store}>
        <Router history={browserHistory} >{routes}</Router>
    </Provider>,
    document.getElementById('app')
);

----编辑-----

import { browserHistory } from 'react-router'
import * as loginTypes from '../lib/constants/login'

export function login(){

    return (dispatch, getState) => {

        var message = "hellooww world"
        dispatch({ message, type: loginTypes.LOGIN })
        browserHistory.push('/login');

    }
}