gpt4 book ai didi

javascript - 为什么这个 action.type 没有在我的 React/Redux 应用程序的 Reducer 中触发?

转载 作者:行者123 更新时间:2023-11-30 07:01:58 25 4
gpt4 key购买 nike

完全被难住了,我有一个设置状态的操作 - isAuthenticated bool 值和基于用户是否登录的用户对象。

Action 肯定会触发,auth reducer 会正确运行,但案例不会被触发,所以状态不会更新:

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

更新该操作在登录时被调用,也在根 index.js 文件中调用:

store.dispatch(setCurrentUser(jwt.decode(localStorage.getItem('token'))))

店铺设置如下:索引.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

根 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'

关于javascript - 为什么这个 action.type 没有在我的 React/Redux 应用程序的 Reducer 中触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42647764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com