为什么这个action.type没有在我的React / Redux应用程序的Reducer中触发?

时间:2017-03-07 12:00:18

标签: javascript reactjs redux

完全难倒,我有一个动作,根据用户是否登录设置状态 - isAuthenticated布尔和用户对象。

操作肯定会触发,auth reducer正在运行,但是没有触发案例,因此状态未更新:

动作:

export const SET_CURRENT_USER = 'SET_CURRENT_USER'

export function setCurrentUser (user) {
  console.log(SET_CURRENT_USER)
  return {
    type: SET_CURRENT_USER,
    user
  }
}

减速

import isEmpty from 'lodash/isEmpty'
import SET_CURRENT_USER from '../actions/authActions'

const INITIAL_STATE = {
  isAuthenticated: false,
  user: {}
}

export default function (state = INITIAL_STATE, action) {

      // console.log('action.type = ', action.type) <<< This is outputting the correct action type: SET_CURRENT_USER
      switch (action.type) {
        case SET_CURRENT_USER:
          console.log('set cur user reducer') // this is not running
          console.log(!isEmpty(action.user)) // this is not running
          return {
            isAuthenticated: !isEmpty(action.user),
            user: action.user
          }
        default:
          return state
      }
    }

更新 该操作在登录时调用,也在根index.js文件中调用:

store.dispatch(setCurrentUser(jwt.decode(localStorage.getItem('token'))))

商店设置如下: Index.js:

const store = configureStore()

configureStore.js:

import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'

import rootReducer from './reducers/index'

const configureStore = () => {
  const store = createStore(
    rootReducer,
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
    applyMiddleware(thunk)
  )
  return store
}

export default configureStore

Root Reducer:

import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'

import auth from './auth'
import transactions from './transactions'
import expenditure from './expenditure'
import income from './income'

const rootReducer = combineReducers({
  auth,
  transactions,
  expenditure,
  income,
  routing: routerReducer
})

export default rootReducer

1 个答案:

答案 0 :(得分:11)

它不会导出为默认值,因此应使用花括号

导入
import { SET_CURRENT_USER } from '../actions/authActions'