Generate Same Random Color with Page refresh in React

Viewed 120

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)),

1 Answers
Related