React Js中循环ant设计标签颜色
我使用 Ant Design 和来自 API 的数据。我假设像这样的数据
data = [
{
name: "John",
job: "Freelancer",
},
{
name: 'Bob',
job: 'UI Designer'
},
{
name: 'Sam',
job: 'CEO'
},
{
name: 'Alex',
job: 'Mobile Dev'
},
{
name: 'Jess',
job: 'Web Dev'
},
];
我想用 Ant Design 的标签返回作业,该标签具有不同的颜色
<Tag color="green">green</Tag>
<Tag color="cyan">cyan</Tag>
我已经循环了数据。但我不知道如何使数据具有不同的颜色标签
data.map((el) => {
//This example doesn't have color
return <Tag>{el.job}</Tag>
})
如何实现?抱歉我的英语不够好。我希望你明白我的意思 或者您可以访问代码沙箱此处
I use Ant Design and data which coming from API. I assume the data like this
data = [
{
name: "John",
job: "Freelancer",
},
{
name: 'Bob',
job: 'UI Designer'
},
{
name: 'Sam',
job: 'CEO'
},
{
name: 'Alex',
job: 'Mobile Dev'
},
{
name: 'Jess',
job: 'Web Dev'
},
];
I want to return the job with Tag from Ant Design which the tag has a different color
<Tag color="green">green</Tag>
<Tag color="cyan">cyan</Tag>
I have looped the data. but I don't know how to make the data have a different color tag
data.map((el) => {
//This example doesn't have color
return <Tag>{el.job}</Tag>
})
How to achieve that ? And sorry i'm not good enough in English. I hope you understand what i mean
Or you can visit code sandbox here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的数据中,为每个对象添加一个类似 tagColor 的属性。
然后在循环中,使用该属性动态添加颜色。就像,
更新
如果颜色可以是随机的,您可以将所有颜色放入一个数组中。您可以使用索引甚至随机化器逐一选择颜色。就像,
它会根据索引在数组中一一挑选颜色。
In your data, add a property something like tagColor for each object.
Then in the loop, use that property to dynamically add colors. Like,
Updated
If the colors can be random, you can place all your colors in an array. And you can pick colors one by one using index or even a randomiser. Something like,
It will pick colors in the array one by one based on index.