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

282
Views
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 answers
Answer question

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 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!