Reactjs 给每个帖子一个不寻常的id
大家好,我在我的网站上有一些静态内容,想制作一些动态内容,所以我有5条不同的新闻,想给他们一个唯一的ID,例如,当你阅读更多你必须得到的信息时Reactjs 给每个帖子一个不寻常的id,reactjs,next.js,Reactjs,Next.js,大家好,我在我的网站上有一些静态内容,想制作一些动态内容,所以我有5条不同的新闻,想给他们一个唯一的ID,例如,当你阅读更多你必须得到的信息时 id:2 title: President-Elections 所以我试着做一些数组 const posts = [ {id: 1, title: "some title", content: "some content"}, {id: 2, title: "some title2", content: "some content2"}, {id
id:2 title: President-Elections
所以我试着做一些数组
const posts = [
{id: 1, title: "some title", content: "some content"},
{id: 2, title: "some title2", content: "some content2"},
{id: 3, title: "some title3", content: "some content3"},
{id: 4, title: "some title4", content: "some content4"},
{id: 4, title: "some title5", content: "some content5"},
];
现在我想分配每个帖子的唯一id,但不知道如何做。您可以使用
uuid
生成器 您可以使用uuid
生成器
函数generateUID(){//Public Domain/MIT
var d=新日期().getTime();
if(性能类型!==“未定义”&&typeof performance.now===“函数”){
d+=performance.now();//如果可用,请使用高精度计时器
}
返回“xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxx”。替换(/[xy]/g,函数(c){
var r=(d+Math.random()*16)%16 | 0;
d=数学楼层(d/16);
返回(c=='x'?r:(r&0x3 | 0x8)).toString(16);
});
}
函数generateUID(){//Public Domain/MIT
var d=新日期().getTime();
if(性能类型!==“未定义”&&typeof performance.now===“函数”){
d+=performance.now();//如果可用,请使用高精度计时器
}
返回“xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxx”。替换(/[xy]/g,函数(c){
var r=(d+Math.random()*16)%16 | 0;
d=数学楼层(d/16);
返回(c=='x'?r:(r&0x3 | 0x8)).toString(16);
});
}
你可以像你以前那样手动给他们手动ID。我需要像发电机一样的“智能”功能。我将通过地图功能尝试这一点,但它给了我一切。这应该有助于你可以像你以前需要“智能”一样手动给他们手动ID像生成器一样的函数。我会通过映射函数来尝试这个,但它给了我所有这些应该可以帮助你,但是如果我需要自己写它?为什么你需要自己写它?如果我需要自己写它,为什么你需要自己写它?