在我的React应用中,我的状态是:
const [functions, setfunctions] = useState([{}]);
未定义的测试:
if (typeof functions[functionCount].url !== "undefined") {
imgSrc = functions[functionCount].url;
} else {
imgSrc = "";
}
还有一个JSX元素(如果有的话)显示URL:
<img style={{ width: "100%" }} src={imgSrc}></img>
我怎么仍会收到此错误?
答案 0 :(得分:1)
首先检查functions[functionsCount]
是否存在-
if (functions[functionCount] && typeof functions[functionCount].url !== "undefined")