Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
Create a multi-line string from a set in React javascript

I have a React function where I am having a set and I want to update an HTML element using the data present in the set. Following is the code example:

const myFunc = (mySet) => {
    document.getElementById('myId').innerHTML = Array.from(mySet).join(' ');
} 

...
// In the render code:
<p id="myId> </p>

This correctly joins the set elements using a blank space separator, but I want to have it displyed in the form of bulleted lists or in a new line.

Is it possible to have multiline append using innerHTML?

Ex: mySet = ('Apple', 'Banana', 'Carrot', 'Drumstick')

Output on my code: Apple Banana Carrot Drumstick

Output desirable:

Apple

Banana

Carrot

Drumstick

(or)

  • Apple
  • Banana
  • Carrot
  • Drumstick
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can achieve it with a little modification on join and change innerHTML to insertAdjacentHTML

const myElement = document.getElementById('myId')
myElement.insertAdjacentHTML('afterBegin',Array.from(mySet).join('<br/>'));

But in my opinion, you should not manipulate DOM with document directly that is React's anti-pattern.

I'd suggest you use states to update your component

const [currentSet, setCurrentSet] = useState([])

const myFunc = (mySet) => {
    setCurrentSet(mySet);
} 

return <div id="myId">currentSet.map((item) => <p>{item}</p>)</div>
about 4 years ago · Juan Pablo Isaza Report

0

If mySet is an Array you should use React to render your items:

const myFunc = (mySet) => {
  
return(
   <ul>
      {mySet && mySet.map(val=>{return (<li>{val}</li>)})}
  </ul>
)

} 
about 4 years ago · Juan Pablo Isaza Report

0

If you are using innerText you could try:

Array.from(mySet).join('\n')

But if you are using innerHtml then try this:

Array.from(mySet).join('<br/>')

or you can map items with tag:

Array.from(mySet).map(function(el) {return '<div>' + el + '</div>';}).join(' ')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!