如何为react-router-redux触发LOCATION_CHANGE事件?

时间:2016-07-08 23:25:28

标签: javascript redux react-router react-router-redux

根据the documentation,我应该可以通过致电LOCATION_CHANGE来发起push('/with/the/path')事件。来自docs的示例:

import { routerMiddleware, push } from 'react-router-redux'

// Apply the middleware to the store
const middleware = routerMiddleware(browserHistory)
const store = createStore(
  reducers,
  applyMiddleware(middleware)
)

// Dispatch from anywhere like normal.
store.dispatch(push('/foo'))

然而,这看起来就像是我打电话给我而得到这个(这看起来不像是在做什么):

enter image description here

我缺少什么?

1 个答案:

答案 0 :(得分:2)

确保将您的历史记录与商店同步。以下是一个示例设置:

import { routerMiddleware, syncHistoryWithStore, push } from 'react-router-redux'

// Apply the middleware to the store
const router = routerMiddleware(browserHistory)
const store = createStore(
  reducers,
  applyMiddleware(router)
)
const history = syncHistoryWithStore(browserHistory, store)

// Dispatch from anywhere like normal.
store.dispatch(push('/foo'))

在创建商店之后,请注意关于syncHistoryWithStore 的位。

希望这有帮助。