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

110
Visualizações
i'm wanting onChange to keep generate id ReactJS

I have function makeid

const makeid = (length) => {
  var result = "";
  var characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};

and i have function createArray use makeid

const createArray = (x) => {
  let array = [];
  const object = {
    id: makeid(5),
  };
  for (let i = 1; i <= x; i++) {
    array.push(object);
  }
  return array;
};

& I create func nummberOfColumns

let a = [];
[...studentObject, ...createArray((5 -studentObject.length)].map((ele) => {
      a.push({
        id: ele.id,
        });
});
return a

then I use number Of Columns.map() to return the </th> tag. In this tag I use onChange, but every time onChange the id changes to another id, I don't want that to happen. I tried using Usememo but it didn't work.

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

0

Use useEffect with an empty dependencies array ([]). There, initiate all your code and attach the result to a state. You can use useState to manage that. It's happening because on each change in a tree, all the subtree re-renders. useEffect with an empty array only runs after mounting and first update (layout and paint). If you want to use useMemo, make sure to pass an empty array as well to prevent from re-calculation when a dependency changes.

useEffect(function(), [])

useMemo(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