使用 map 在 2 个循环后添加 类
Add the classes after 2 loops on react using map
我正在尝试在 2 个循环后添加 类。它正在运行,但无法隐藏 {x++}
文本。这怎么能隐瞒?我也尝试过使用索引。
const workdata = this.state.worksData.map((work, index) => (
<div
className={
x % 3 == 0
? "col-lg-4 col-md-6 offset-lg-0 offset-md-3"
: "col-lg-4 col-md-6"
}
key={index}
>
<div
className={
x % 3 == 0 ? "single-box" : "single-box with-line"
}
>
<span>{work.position}</span>
</div>
{x++}
</div>
));
使用 for 循环并有效。
for (let i = 0; i < data.length; i++) {
if (x % 3 == 0) {
console.log("Class", data[i]);
} else {
console.log(data[i]);
}
x++;
}
您应该使用现有索引 属性 而不是创建新的 x。请看下面的示例代码
const workdays = this.state.worksData.map((work, index) => (
<div
className={
(index + 1) % 3 == 0
? "col-lg-4 col-md-6 offset-lg-0 offset-md-3"
: "col-lg-4 col-md-6"
}
>
<div
className={
(index + 1) % 3 == 0 ? "single-box" : "single-box with-line"
}
>
<span>{work.position}</span>
</div>
</div>
));
我正在尝试在 2 个循环后添加 类。它正在运行,但无法隐藏 {x++}
文本。这怎么能隐瞒?我也尝试过使用索引。
const workdata = this.state.worksData.map((work, index) => (
<div
className={
x % 3 == 0
? "col-lg-4 col-md-6 offset-lg-0 offset-md-3"
: "col-lg-4 col-md-6"
}
key={index}
>
<div
className={
x % 3 == 0 ? "single-box" : "single-box with-line"
}
>
<span>{work.position}</span>
</div>
{x++}
</div>
));
使用 for 循环并有效。
for (let i = 0; i < data.length; i++) {
if (x % 3 == 0) {
console.log("Class", data[i]);
} else {
console.log(data[i]);
}
x++;
}
您应该使用现有索引 属性 而不是创建新的 x。请看下面的示例代码
const workdays = this.state.worksData.map((work, index) => (
<div
className={
(index + 1) % 3 == 0
? "col-lg-4 col-md-6 offset-lg-0 offset-md-3"
: "col-lg-4 col-md-6"
}
>
<div
className={
(index + 1) % 3 == 0 ? "single-box" : "single-box with-line"
}
>
<span>{work.position}</span>
</div>
</div>
));