在我的应用程序中,我为相同uniqueID
的背景色指定了相同的随机 colored颜色 ,如果页面重新加载,我还希望此 colored颜色 为相同的随机 colored颜色 .
我的方法和当前的行为与前一篇文章中的解决方案相似:Generate Random Same Colors in React然而,colors change if the page is reloaded.
Question:有没有办法将uniqueID
映射为随机 colored颜色 ,并使用REACT保存我的应用程序的不同实例?
请参见以下我的当前代码:
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)),