redux reducer 返回未定义处理
redux reducer returned undefined handling
我的减速器喜欢:
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
获取操作对象,但这给了我错误提示 .. Uncaught Error: Reducer "login" returned undefined handling "LOGIN". To ignore an action, you must explicitly return the previous state.
为什么会出现此错误?
替换:
return
[
收件人:
return [
因为first就像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 });
};
我的减速器喜欢:
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
获取操作对象,但这给了我错误提示 .. Uncaught Error: Reducer "login" returned undefined handling "LOGIN". To ignore an action, you must explicitly return the previous state.
为什么会出现此错误?
替换:
return
[
收件人:
return [
因为first就像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 });
};