如何解决react.js 中选择器的问题如果我在 react.js 中使用选择器并且如果我在其中一个组件中导出它会在该选择器部分显示错误
export const initialState = {
basket: [],};
// reducer takes care about adding and removing items from the basket
// selector
export const getBasketTotal = (basket) =>
basket?.reduce((amount,item) => item.price + amount,0);
// the above code helps us tally the total amount of products
const reducer = (state,action) => {
switch (action.type) {
case "ADD TO BASKET":
return {
...state,basket: [...state.basket,action.items],};
// the above code helps us to push items to the basket with current elemnts in the basket
default:
return state;
}
};
export default reducer;
它显示的错误是export const getBasketTotal,它显示的错误是匿名函数
解决方法
选择器通常接收整个状态对象。我怀疑您想访问 state.basket
。
export const getBasketTotal = (state) =>
state.basket?.reduce((amount,item) => item.price + amount,0);
我觉得还有点可疑的是 "ADD TO BASKET"
操作:
basket: [...state.basket,action.items],
action.items
IMO 意味着多个项目,因此我认为您可能希望将项目分散到 basket
数组中。
basket: [...state.basket,...action.items],
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。