kibowki
kibowki

Reputation: 4376

How do I listen for react-router location change in redux reducer?

I have a React Redux app, and am trying to listen for a react-router location change in one of my reducers. I am using hashHistory, not browserHistory. In my Redux devtools, I can see it is firing an action when the location changes:

enter image description here

However, in my reducer, when I listen for 'LOCATION_CHANGE', it doesn't catch anything.

import * as types from '../constants/actionTypes';
import objectAssign from 'object-assign';
import initialState from './initialState';

export default function listingsReducer(state = initialState.listings, action) {
  switch (action.type) {
    case 'LOCATION_CHANGE': {
        console.log(action); //currently doesn't get into this case block
        return state;
    }
    default:
        return state;
  }
}

What do I have to use in order to handle this action in my reducer?

Upvotes: 3

Views: 7345

Answers (2)

Cethy
Cethy

Reputation: 1636

Even better, use the constant provided by react-router :

import { LOCATION_CHANGE } from 'connected-react-router';

// ...

switch (action.type) {
    case LOCATION_CHANGE: {}
}

Upvotes: 9

Jim Bolla
Jim Bolla

Reputation: 8295

Change case 'LOCATION_CHANGE' to case '@@router/LOCATION_CHANGE'.

Upvotes: 8

Related Questions