如何在 next.js 的数组映射函数中使用 if 语句
How to use if statement in array map function with next.js
如果索引号的余数为0,我想包装我需要在下面和下面显示的代码。我怎样才能做到这一点?我尝试了下面的方法,但没有用。我收到语法错误。
{index%3 == 0? ...: ...}
{index% 3 == 0 && ...}
export default function UserPosts() {
// some code...
return (
<div className={styles.userPosts}>
{postsList.map((post, index) => {
return (
if (index % 3 == 0) {
<div className={styles.userPostsRow}>
}
<div className={styles.userPostWrapper}>
<div className={styles.userPostColumn}>
<Link href={`/${username}`}>
<a>
<div className={styles.userPost}>
<img src={post.image} alt="" />
</div>
</a>
</Link>
</div>
</div>
if (index % 3 == 0) {
</div>
}
)
})}
</div>
)
}
在你的 return
语句中,你需要使用 JSX。 JSX 中的条件可以采用不同的形式,这些是我更喜欢的形式:
如果存在(隐式&&
):
{ arr.map( (item) => {
return(
item.condition &&
<div className="whatever">Your Content</div>
)
})}
如果满足条件(条件为&&
)
{ arr.map( (item, index) => {
return(
item.index % 3 &&
<div className="whatever">Your Content</div>
)
})}
If/else块(使用三进制? () : ()
)
{ arr.map( (item, index) => {
return(
item.index % 3 ? (
<>
<div className="whatever">True catch condition</div>
<div className="whatever">Multiple HTML lines</div>
</>
) : (
<div className="whatever">False catch condition</div>
)
)
})}
为了减少 JSX 重复,您可以将条件逻辑提取到一个组件中,该组件将包装子组件。
const ConditionalWrapper = ({ children, condition }) => {
return condition ? (
<div className={styles.userPostsRow}>{children}</div>
) : (
children
)
}
export default function UserPosts() {
// some code...
return (
<div className={styles.userPosts}>
{postsList.map((post, index) => {
return (
<ConditionalWrapper condition={index % 3 == 0}>
<div className={styles.userPostWrapper}>
<div className={styles.userPostColumn}>
<Link href={`/${username}`}>
<a>
<div className={styles.userPost}>
<img src={post.image} alt="" />
</div>
</a>
</Link>
</div>
</div>
</ConditionalWrapper>
)
})}
</div>
)
}
如果索引号的余数为0,我想包装我需要在下面和下面显示的代码。我怎样才能做到这一点?我尝试了下面的方法,但没有用。我收到语法错误。
{index%3 == 0? ...: ...}
{index% 3 == 0 && ...}
export default function UserPosts() {
// some code...
return (
<div className={styles.userPosts}>
{postsList.map((post, index) => {
return (
if (index % 3 == 0) {
<div className={styles.userPostsRow}>
}
<div className={styles.userPostWrapper}>
<div className={styles.userPostColumn}>
<Link href={`/${username}`}>
<a>
<div className={styles.userPost}>
<img src={post.image} alt="" />
</div>
</a>
</Link>
</div>
</div>
if (index % 3 == 0) {
</div>
}
)
})}
</div>
)
}
在你的 return
语句中,你需要使用 JSX。 JSX 中的条件可以采用不同的形式,这些是我更喜欢的形式:
如果存在(隐式&&
):
{ arr.map( (item) => {
return(
item.condition &&
<div className="whatever">Your Content</div>
)
})}
如果满足条件(条件为&&
)
{ arr.map( (item, index) => {
return(
item.index % 3 &&
<div className="whatever">Your Content</div>
)
})}
If/else块(使用三进制? () : ()
)
{ arr.map( (item, index) => {
return(
item.index % 3 ? (
<>
<div className="whatever">True catch condition</div>
<div className="whatever">Multiple HTML lines</div>
</>
) : (
<div className="whatever">False catch condition</div>
)
)
})}
为了减少 JSX 重复,您可以将条件逻辑提取到一个组件中,该组件将包装子组件。
const ConditionalWrapper = ({ children, condition }) => {
return condition ? (
<div className={styles.userPostsRow}>{children}</div>
) : (
children
)
}
export default function UserPosts() {
// some code...
return (
<div className={styles.userPosts}>
{postsList.map((post, index) => {
return (
<ConditionalWrapper condition={index % 3 == 0}>
<div className={styles.userPostWrapper}>
<div className={styles.userPostColumn}>
<Link href={`/${username}`}>
<a>
<div className={styles.userPost}>
<img src={post.image} alt="" />
</div>
</a>
</Link>
</div>
</div>
</ConditionalWrapper>
)
})}
</div>
)
}