Reducer not catching LOCATION_CHANGE action
Asked Answered
N

2

7

I'm working toward having my React/Redux app update the URL based on actions. I've done quite a bit of looking around. I thought I had a handle on it, but obviously I'm missing something. I have other reducers that respond correctly.

Currently, I'm just trying to log the action.

Routing Reducer

const initialState = { locationBeforeTransitions: null };

export default function routing(state = initialState, action)
{
  switch (action.type)
  {
    case 'LOCATION_CHANGE':
      console.log(action)

    default:
      return state

  }

}

Store

/*
  Things from other people
 */

import { createStore, applyMiddleware, compose } from 'redux'
import { syncHistoryWithStore }                  from 'react-router-redux';
import { browserHistory }                        from 'react-router'
import   thunkMiddleware                         from 'redux-thunk'
import   createLogger                            from 'redux-logger'

/*
  Things from us
 */

import { fetchSuitesAndFails, fetchResults } from './actions/actions';
import   rootReducer                         from './reducers/index'

const enhancers = compose(
  window.devToolsExtension ? window.devToolsExtension() : f => f
);

const loggerMiddleware = createLogger()

/*
  Store
*/

export const store = createStore(
  rootReducer,
  enhancers,
  applyMiddleware(
    thunkMiddleware, // lets us dispatch() functions
    loggerMiddleware // neat middleware that logs actions
  )
);

// Export the history so we can keep track of things
export const history = syncHistoryWithStore(browserHistory, store);


/*
  Enable Hot Reloading for the reducers
  We re-require() the reducers whenever any new code has been written.
  Webpack will handle the rest
*/

if (module.hot) {
  // Enable Webpack hot module replacement for reducers
  module.hot.accept('./reducers/index', () => {
    const nextRootReducer = require('./reducers/index').default
    store.replaceReducer(nextRootReducer)
  })
}

Index

/*
  React router needs
*/
import { combineReducers } from 'redux'
import { routerReducer }   from 'react-router-redux'

/*
  Reducers
*/
import suite          from './suite'
import suitesandfails from './suitesandfails'
import routing        from './routing'


/*
  Put them all together and return a single reducer
*/
const rootReducer = combineReducers({
  suite,
  suitesandfails,
  routing,
  routing: routerReducer
})

export default rootReducer
Needs answered 6/8, 2016 at 7:15 Comment(7)
have you added routing to the rootReducer?Declassify
I did. I updated to show.Needs
What code is dispatching the action? There could be a bug there before the reducer.Tympanist
I have never assigned two reducers to the same property in combineReducers. Is the first reducer not getting called because of double assignment? Not sure though.Declassify
React-router-redux dispatches the action. It shows up in the Dev tools.Needs
you should rename it.Vulture
It's true that I should rename it, but still no dice when I do.Needs
V
11

you can use string "@@router/LOCATION_CHANGE" to catch the action.

react-router-redux provides const for that

import { LOCATION_CHANGE } from 'react-router-redux'

....
case LOCATION_CHANGE:
      console.warn('LOCATION_CHANGE from your reducer',action)
      return state

webpackbin DEMO

enter image description here

routerReducer code from react-router-redux

Vulture answered 6/8, 2016 at 11:43 Comment(0)
M
3

You can also import the constant from the 'react-router-redux'.

import { LOCATION_CHANGE } from 'react-router-redux'
 ...
 case LOCATION_CHANGE:
     console.warn('LOCATION_CHANGE from your reducer',action)
     return state
Murton answered 29/11, 2016 at 23:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.