totally stumped, I have an action which sets a state - isAuthenticated boolean and user object based on whether a user is logged in or not.
The action is definitely firing, the auth reducer is correctly running, but the case isn't being triggered so the state isn't updated:
Action:
export const SET_CURRENT_USER = 'SET_CURRENT_USER'
export function setCurrentUser (user) {
console.log(SET_CURRENT_USER)
return {
type: SET_CURRENT_USER,
user
}
}
Reducer
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 The action is being called on login, and also in the root index.js file with:
store.dispatch(setCurrentUser(jwt.decode(localStorage.getItem('token'))))
Store setup is as follows: 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
它不会默认导出,因此应使用花括号导入
import { SET_CURRENT_USER } from '../actions/authActions'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.