redux reducer返回未定义的处理
我的减速机如下:redux reducer返回未定义的处理,redux,reducers,Redux,Reducers,我的减速机如下: const initialState = [ { fname: null, lname: false, } ] export default function login(state = initialState, action) { switch (action.type) { case LOGIN: console.log("actions") console.log(action) console.
const initialState = [
{
fname: null,
lname: false,
}
]
export default function login(state = initialState, action) {
switch (action.type) {
case LOGIN:
console.log("actions")
console.log(action)
console.log("reducers")
return
[{
fname: action.fname,
lname: action.lname,
}]
default:
return state
}
}
这里我得到了带有fname
和lname
的action对象,但这给了我一个错误提示<代码>未捕获错误:Reducer“login”返回未定义的处理“login”。要忽略某个操作,必须显式返回上一个状态。
为什么会出现此错误?替换:
return
[
致:
因为第一个类似于返回代码>(js在行尾添加;
)或
...
console.log("reducers")
return([{
fname: action.fname,
lname: action.lname,
}])
//example
function getPerson(){
return([{
fname: 'jay',
lname: 'dawg'
}])
}
console.log(getPerson()[0].lname) // "dawg"
对我来说,这是因为我忘了让动作调用异步async
export const asyncFetchUser = () => async (dispatch) => {
const response = *await* axios.get('http://localhost:5000/api/current_user');
dispatch({ type: ASYNC_FETCH_USER, auth: response.data });
};
export const asyncFetchUser = () => async (dispatch) => {
const response = *await* axios.get('http://localhost:5000/api/current_user');
dispatch({ type: ASYNC_FETCH_USER, auth: response.data });
};