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

284
Visualizações
Generate Same Random Color with Page refresh in React

In my application, I assign the same random color for the background color of the same uniqueID and I also want this color to be the same random color if the page reloads.

My approach and current behavior is similar to the solution in this prior post: Generate Random Same Colors in React however, the colors change if the page is reloaded.

Question: Is there a way to map the uniqueID to a random color, and have it persist different instances of my app using REACT?

See below my current code:

function getRandomColor(): string{
  return '#' + Math.floor(Math.random()*16777215).toString(16);
}
let colorMap = new Map<number, string>();
function setColor(uniqueId: number): string{
  if(colorMap.get(uniqueId) == null){
    colorMap.set(uniqueId,getRandomColor());
  }
  return colorMapping.get(id) + "";
}
//later in the code
color: setColor(Number(d_id)),

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

0

You could use one of the various storage options to store this and then restore on each page load:

  • Cookies
  • Session
  • LocalStorage
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