REDUX: Error: Actions may not have an undefined "type" property. Have you misspelled a constant?
REDUX: Error: Actions may not have an undefined "type" property. Have you misspelled a constant?
我正在学习 Redux,我对这里发生的事情感到很困惑。我正在使用 thunk 并且 GET_ITEMS 在我的减速器中所以我不确定我做错了什么?错误在 dispatch(getItemsAction());
Redux.js
function reducer(state, action) {
switch (action.type) {
case 'GET_ITEMS':
return {
...state,
items: action.payload,
loading: false,
};
case 'ADD_ITEM':
return {
...state,
items: [...state.items, action.payload],
};
case 'DELETE_ITEM':
return {
...state,
items: state.items.filter(item => item.id !== action.payload),
};
case 'ITEMS_LOADING':
return {
...this.state,
loading: true,
};
default:
return state;
}
}
export const getItemsAction = () => ({
return(dispatch) {
axios.get('api/items').then(response => {
console.log(response);
dispatch({ type: 'GET_ITEMS', payload: response.data });
});
},
});
ShoppingList.js
import { addItemAction, deleteItemAction, getItemsAction } from '../redux';
export default function ShoppingList() {
const items = useSelector(state => state.items);
const dispatch = useDispatch();
const addItem = name => dispatch(addItemAction(name));
const deleteItem = id => dispatch(deleteItemAction(id));
useEffect(() => {
dispatch(getItemsAction());
}, []);
在顶部代码中,您 return以错误的方式进行了调度
但实际上你需要像 cb 这样调用 dispatch
例如在 javascript 我们做这样的事情
const myfunc = () => cb => {
cb('OK')
};
它在 javascript 中的回调,你必须 return 分派类似回调才能正常工作
export const getItemsAction = () => dispatch => {
axios.get('api/items').then(response => {
dispatch({
type: 'GET_ITEMS',
payload: response.data
})
});
};
最后别忘了用response.data
获取axios响应数据
该操作的正确语法是
export const getItemsAction = () => dispatch => {
axios.get('/api/items').then(res =>
dispatch({
type: 'GET_ITEMS',
payload: res.data,
})
);
};
我正在学习 Redux,我对这里发生的事情感到很困惑。我正在使用 thunk 并且 GET_ITEMS 在我的减速器中所以我不确定我做错了什么?错误在 dispatch(getItemsAction());
Redux.js
function reducer(state, action) {
switch (action.type) {
case 'GET_ITEMS':
return {
...state,
items: action.payload,
loading: false,
};
case 'ADD_ITEM':
return {
...state,
items: [...state.items, action.payload],
};
case 'DELETE_ITEM':
return {
...state,
items: state.items.filter(item => item.id !== action.payload),
};
case 'ITEMS_LOADING':
return {
...this.state,
loading: true,
};
default:
return state;
}
}
export const getItemsAction = () => ({
return(dispatch) {
axios.get('api/items').then(response => {
console.log(response);
dispatch({ type: 'GET_ITEMS', payload: response.data });
});
},
});
ShoppingList.js
import { addItemAction, deleteItemAction, getItemsAction } from '../redux';
export default function ShoppingList() {
const items = useSelector(state => state.items);
const dispatch = useDispatch();
const addItem = name => dispatch(addItemAction(name));
const deleteItem = id => dispatch(deleteItemAction(id));
useEffect(() => {
dispatch(getItemsAction());
}, []);
在顶部代码中,您 return以错误的方式进行了调度 但实际上你需要像 cb 这样调用 dispatch 例如在 javascript 我们做这样的事情
const myfunc = () => cb => {
cb('OK')
};
它在 javascript 中的回调,你必须 return 分派类似回调才能正常工作
export const getItemsAction = () => dispatch => {
axios.get('api/items').then(response => {
dispatch({
type: 'GET_ITEMS',
payload: response.data
})
});
};
最后别忘了用response.data
获取axios响应数据该操作的正确语法是
export const getItemsAction = () => dispatch => {
axios.get('/api/items').then(res =>
dispatch({
type: 'GET_ITEMS',
payload: res.data,
})
);
};