从react-router-redux获取当前位置

Don*_*n P 5 reactjs react-router redux react-redux react-router-redux

使用"react-router-redux"时如何获得当前位置?

这是routing州:

{
    routing: {
        locationBeforeTransition: {
            pathname: "/foo",
            search: "",
            hash: "",
            state: null,
            action: "PUSH",
            key: "e8jfk"
        },
        query: null,
        $searchBase: {
            search: "",
            searchBase: ""
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以清楚地访问当前位置state.routing.locationBeforeTransition.pathname,但名称"locationBeforeTransition"似乎是上一页的位置,而不是当前位置.这似乎很奇怪,这是我的路由状态中唯一的属性.我怀疑我做错了什么.

这是一个只有路由的简化reducer:

reducer.js

import { combineReducers, createStore, applyMiddleware, compose } from 'redux';
import { routerReducer, routerMiddleware } from 'react-router-redux';
import { browserHistory } from 'react-router';
import thunkMiddleware from 'redux-thunk';

const reducer = combineReducers({ 
  routing: routerReducer, 
});

export const store = createStore(reducer, {}, compose(
    applyMiddleware(
      routerMiddleware(browserHistory), 
      thunkMiddleware
    ),
    window.devToolsExtension ? window.devToolsExtension() : f => f
  )
);
Run Code Online (Sandbox Code Playgroud)

Ori*_*ori 4

您实际上无法从 redux 存储中获取它,因为根据How do I access router state in a container component? 中的react-router-redux 文档

您不应直接从 Redux 存储中读取位置状态。这是因为 React Router 是异步运行的(以处理动态加载组件之类的事情),并且您的组件树可能尚未与 Redux 状态同步更新。您应该依赖 React Router 传递的 props,因为它们仅在处理完所有异步代码后才会更新。