1

I am new to react and using .map() to list some items with a
tag appended to it. The issue that I am facing is when i append a
tag it doesn't do a line break. It just shows
next to every store. Below is my code i am using. Any help would be really appreicated.

   return <div> {stores.map((store) => store + "<br>")} </div>
2
  • 2
    Because it's just a string, not an element. Try e.g. <>{store}<br/></>, otherwise you have to "dangerously set inner HTML". Commented Nov 26, 2020 at 19:46
  • No, you can't (shouldn't) add html. However, you can return jsx elements from the map callback. Commented Nov 26, 2020 at 22:48

1 Answer 1

3

Of course you can. But you should wrap them inside one tag e.g. <div> or <>.

return (<div>
         {stores.map((store) => (
             <>{store}<br></> // You can also use `<div>{store}</div>` since `div` is achieving the br effect.
         ))}
       </div>)
Sign up to request clarification or add additional context in comments.

1 Comment

Wrapping them in <div>s will not achieve the effect that <br> is supposed to achieve. Also I guess in JSX you'll have to write <br />?

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.