2

I have a const in render function like const arr = ["james","john"];

and in the return function I do

<select>
    arr.map(item =>
    <option>item</option>
    )
</select>

What I see in the output is item, not the value of the array. Am I missing something? I want to populate the array value as options.

1 Answer 1

7

Content inside JSX elements will normally be interpreted as "text", so React treats arr.map(…) as the literal content of the <select> element.

(<option>item</option> is therefore simply treated as the literal child element of <select>; your browser probably ignores the surrouding text, which is why you only see "item" in your list).

When you instead want it to be treated as a JavaScript expression, simply wrap it in curly brackets (as explain in the "Introducing JSX" page of React's quick start guide):

<select>
  {arr.map(item =>
    <option>{item}</option>
  )}
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

Now I got 3 "item". I think you missed the curly bracket on the item.
@JessicaRobertson You're right, I've updated so it should be correct now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.