最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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:

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?

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:

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?

Share Improve this question asked Oct 28, 2016 at 20:19 kibowkikibowki 4,37616 gold badges51 silver badges79 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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

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

// ...

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

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

发布评论

评论列表(0)

  1. 暂无评论