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

184
Views
If I have a useState which has a value of 4, how can I use the below function to create the returned HTML 4 times using the value? (React)

I am trying to create a quiz app which at the end of each round creates a table which contains the players score as well as the score of a set number of random players. Lets say the number of random players has been pre selected and is 4. How do I use the value of 4 from the useState to render the below HTML 4 times? I couldn't get for loop to work, maybe someone else can or has another idea?

    function Players () {
      
      return <><tbody>
      <tr>
        <td>{teamName}</td>
        <td class="space"></td>
        <td>{score}</td>
      </tr>
    </tbody></>
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can create an array of desired length using Array.from and fill it with the Players component and then render the array.

function App() {
  const [count, setCount] = useState(4);

  return (
    <div>
      {Array.from({ length: count }, () => (
        <Players />
      ))}
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

you can use Array constructor to do this.

Code:

const [number, setNumber] = useState(4);

{
   [...Array(number)].map((_, index) => (
    <div className="" key={index}> Whatever needs to be repeatedly </div>
       )
    )
 }
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!