The issue is that you're initializing apiData
as an array, rather than an object. Here's the issue, demonstrated:
let x = [];
const { property } = x; // property === undefined!
console.log(property.data) // ERROR! Cannot find property `data` of undefined
The reason this breaks is because when Javascript checks an object for a property that doesn't exist, it returns undefined. However, when you check for a property off of undefined
, JavaScript will throw an error.
This will prevent you from getting the error, and will log undefined
to the console instead of breaking:
const [apiData, setApiData] = useState({ ifaResult: {}, orgResult: {}});
Another Contrived Example:
const x = undefined;
x.data // ERROR!
//...
const x = {}
x.data // undefined
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…