小编owu*_*zan的帖子

如何在 next.js 的数组映射函数中使用 if 语句

如果索引号的余数为0,我想包装我需要在下面和下面显示的代码。我怎样才能做到这一点?我尝试了下面的方法,但没有成功。我收到语法错误。

{索引%3==0?...:...}

{索引% 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>
    )
}
Run Code Online (Sandbox Code Playgroud)

javascript conditional-operator jsx reactjs next.js

4
推荐指数
2
解决办法
3万
查看次数

标签 统计

conditional-operator ×1

javascript ×1

jsx ×1

next.js ×1

reactjs ×1