TypeError:在实际测试未定义的属性时,无法读取未定义的属性“ url”

时间:2020-04-27 19:14:36

标签: javascript reactjs

在我的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>

我怎么仍会收到此错误?

1 个答案:

答案 0 :(得分:1)

首先检查functions[functionsCount]是否存在-

if (functions[functionCount] && typeof functions[functionCount].url !== "undefined")
相关问题