我正在尝试从API访问一些数据,但是当我收到时,我无法读取null的属性“ 5”
import React from "react";
function Card(props) {
console.log(props.data[5].mission_name); //why is it that this doesn't work in my machine?
console.log(props.data); //this works with my machine
return (
<div>
<h1>test</h1>
</div>
);
}
export default Card;
我最初将要传递的数据设置为null,然后一旦从API加载数据就更新了它,因此我认为这可能是问题的一部分,但我不知道如何解决。我为更多上下文创建了一个代码沙箱。作为参考,我正在使用spacex api。
https://codesandbox.io/embed/gallant-mcnulty-lyied?fontsize=14&hidenavigation=1&theme=dark
提前致谢。
如您所说,您最初将数据值设置为null。当Card最初呈现时,它将尝试为空索引5,并为您提供该错误。尝试类似的东西
if (props.data) { // the first render (where data is null) will skip this block
console.log(props.data[5].mission_name);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句