javascript - Reducer 未捕获 LOCATION_CHANGE 操作

标签 javascript reactjs redux react-router react-router-redux

我正在努力让我的 React/Redux 应用根据操作更新 URL。我四处看了看。我以为我已经掌握了它,但显然我遗漏了一些东西。我还有其他正确响应的 reducer 。

目前,我只是想记录操作。

路由 reducer

const initialState = { locationBeforeTransitions: null };

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

    default:
      return state

  }

}

商店

/*
  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)
  })
}

索引

/*
  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

最佳答案

您可以使用字符串 "@@router/LOCATION_CHANGE" 来捕捉 Action 。

react-router-redux 为此提供了 const

从 'react-router-redux' 导入 { LOCATION_CHANGE }

....
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

关于javascript - Reducer 未捕获 LOCATION_CHANGE 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38801601/

相关文章:

javascript - 如何将 props 传递给 `redux-form` 中的装饰表单?

javascript - 可能是我的一个简单的 Jest/ react 错误

javascript - 将两列变成选项卡

javascript - jquery删除除第二个选项值外的 'select option'元素值

javascript - jQuery Mobile - pageinit 与 pageshow

javascript - ES6 父类覆盖子原型(prototype)

javascript - ng-model 将输入字段中的文本更改为 [Object object]

reactjs - 无法创建WebStorm React项目

javascript - D3.js 将烛台图与折线图相结合

reactjs - 在 redux 初始状态和 reducer 中使用嵌套对象