我正在尝试在我的预算管理应用程序中平衡收入/支出,但我的余额组件不断出现此错误:TypeError:无法读取未定义的属性(读取“类型”)。
我认为这个问题可能与我从我的减速器中带来的状态有关,该状态重复了 2 次空数组 [] 和 2 次已满。我尝试了很多但无法修复它..知道它可能是什么吗?
这是我的平衡组件代码:
export default function Balance(){
const operations = useSelector((state) => state.operations);
console.log(operations);
//Shows 4 arrays, 2 empty and 2 full
const [total, setTotal] = React.useState([]);
const operationsListCopy = [...operations];
React.useEffect(() => {
let entryArray = operationsListCopy.filter((oneOperation) => oneOperation.type == 'INCOME');
let entryArrayTotal = entryArray?.reduce((amount, item) => item.amount + amount, 0);
let exitArray = operationsListCopy.filter((oneOperation) => oneOperation.type == 'EXPENSE');
let exitArrayTotal = exitArray?.reduce((amount, item) => item.amount + amount, 0);
let arrayTotal = entryArrayTotal - exitArrayTotal;
setTotal(arrayTotal);
}, [operations]);
您应该在访问之前检查空值或未定义值。你可以在这里添加条件
let entryArray = operationsListCopy.filter((oneOperation) => oneOperation?.type == 'INCOME');
和这里
let exitArray = operationsListCopy.filter((oneOperation) => oneOperation?.type == 'EXPENSE');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句