如何在redux减速机中监听react-router位置变化?

twi*_*lco 1 javascript reactjs react-router redux

我有一个React Redux应用程序,我正试图在我的一个Reducer中监听react-router位置变化.我正在使用hashHistory,而不是browserHistory.在我的Redux devtools中,我可以看到它在位置发生变化时触发了一个动作:

在此输入图像描述

但是,在我的reducer中,当我听'LOCATION_CHANGE'时,它没有捕获任何东西.

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

为了在我的减速机中处理这个动作,我必须使用什么?

Jim*_*lla 6

更改case 'LOCATION_CHANGE'case '@@router/LOCATION_CHANGE'.


Cet*_*thy 5

更好的是,使用提供的常量react-router

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

// ...

switch (action.type) {
    case LOCATION_CHANGE: {}
}
Run Code Online (Sandbox Code Playgroud)