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

172
Visualizações
Issue related to Js/React arrays

I'm creating an array of indexes in which I'm appending values(this will later be used in a map function).

let indexes = [];
export default function SortTiles() {

    let num = info.num_tiles;
    for (let i = 10; i < num+10; i++) {
        indexes = [...indexes, i];
    }
    console.log(indexes);
    console.log("UWU")

    return(
        <div id={"sort-tiles"}>
            {/*{indexes.map(makeTiles)}*/}
        </div>
    )
}

But when I'm trying to console.log my array this is the output I'm getting: The output image The output is being printed twice and the numbers in the array are also duplicated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Not sure what are you trying to achieve but this is not the proper way to handle arrays or any data in react component.

Try this may be this will help you or might fix your problem.

import {useState, useEffect} from 'react';

export default function SortTiles() {

  const [indexes, setIndexes] = useState([]);

  useEffect(() => {
    let num = 20;
    let indexArr = []
    for (let i = 10; i < num+10; i++) {
      indexArr = [...indexArr, i];
    }
    setIndexes(indexArr);
  }, [])


return(
    <div id={"sort-tiles"}>
        {indexes.map((ind) => (<div>{ind}</div>))}
    </div>
)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The array is printed twice most likely because you are in strict mode, which double renders components to help catch bugs.

Lucky for you, it helped you catch one! The values are duplicated because the indexes variable is declared outside the function. Since you never reset it to an empty value, every re-render will just continue adding indexes. Try initializing it inside the function.

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