[英]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.