0

In my react app I have state:

  const [functions, setfunctions] = useState([{}]);

A test for undefined:

if (typeof functions[functionCount].url !== "undefined") {
            imgSrc = functions[functionCount].url;
          } else {
            imgSrc = "";
          }

And a JSX element that displays the URL if there is one:

<img style={{ width: "100%" }} src={imgSrc}></img>

How am I still getting this error?

1
  • 1
    Because it's not .url that's undefined. It's the part before that that's undefined (functions[functionCount]) Commented Apr 27, 2020 at 19:17

1 Answer 1

1

Check for the existence of functions[functionsCount] first -

if (functions[functionCount] && typeof functions[functionCount].url !== "undefined")
Sign up to request clarification or add additional context in comments.

1 Comment

I can't tell you how many times over the last year whilst learning javascript I have banged my head against this issue. Finally I know.. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.