2017-03-07 32 views
0

完全難倒,我有一個行動,設置一個狀態 - isAuthenticated布爾和用戶對象的基礎上,用戶是否登錄或不。爲什麼這個action.type在我的React/Redux應用程序的Reducer中沒有觸發?

動作肯定是射擊中,auth減速運行正常,但這種情況是不被觸發,因此狀態不更新:

操作:

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

UPDATE 動作被稱爲上登錄,並在根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 
+0

? –

+0

嗨@ArshabhAgarwal - 是的,我做到了,正確輸出動作類型字符串。 –

+0

您是如何設置減速機的?你有商店配置代碼嗎? –

回答

8

你能爲SET_CURRENT_USER也做了控制檯日誌它不會導出默認所以應該用進口花括號

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

ARGHHHHHH!哈哈。我一直在盯着這裏一個多小時。謝謝@duwalanise - 明星 –

+0

我的榮幸.... :) – duwalanise

相關問題