Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda