繁体   English   中英

为什么在我的React / Redux App的Reducer中不触发该action.type?

[英]Why is this action.type not firing in the Reducer of my React/Redux App?

完全不知所措,我有一个设置状态的操作-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

根减少剂:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM