完全难倒,我有一个行动,设置一个状态 - 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
? –
嗨@ArshabhAgarwal - 是的,我做到了,正确输出动作类型字符串。 –
您是如何设置减速机的?你有商店配置代码吗? –