Search code examples
javascriptreduxreact-routerreact-router-redux

How do I fire a LOCATION_CHANGE event for react-router-redux?


According to the documentation I should be able to fire a LOCATION_CHANGE event by calling push('/with/the/path'). Example from 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'))

However, this looks like when I call push I'm getting this (which doesn't look like it's doing anything):

enter image description here

What am I missing?


Solution

  • Make sure to sync your history with your store. The following is an example setup:

    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'))
    

    Notice the bit about syncHistoryWithStore after you create the store.

    Hope this helps.