javascript - .map() 中的 If 语句?

标签 javascript reactjs jsx

我想知道如何在 .map() 中实现 if 语句
请参阅下面的代码。
目前,如果当前用户未上传图片,删除按钮将被禁用,但我的目标是完全不呈现删除按钮。

    return <Grid container justify="center" spacing={2}>
        {/* All images */}
        {docs && docs
            // For every image
            .map(image => (
                // In a grid item
                <Grid className="img-item" item key={image.id} xs={12} md={6} lg={4}>
                    {/* all accounts */}
                    {docs2 && docs2
                        // For every single image:
                        // Filter statament only contains the user of specific image
                        // https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d
                        .filter((user) => image.userID === user.userID)
                        //Now you have the user that belongs to the image.ID
                        .map(user => (
                            <div key={image.id}>
                                <img src={image.url} alt="uploaded pic" />
                                <Typography variant="subtitle1"> By {user.userName}

                                    {/* How do I implement this if statement for the IconButton? */}
                                    {/* if ({handleButton(image.userID)} === false){
                                        return 
                                    } */}

                                    {/* Delete button */}
                                    <IconButton
                                        disabled={handleButton(image.userID)}
                                        color="secondary" aria-label="delete image"
                                        onClick={() => handleDeleteImage(image.id, image.userID, image.name)}
                                        component="span" >
                                        <DeleteForever />
                                    </IconButton>

                                </Typography>
                            </div>
                        ))}
                </Grid>
            ))}
    </Grid>
}
export default ImageGrid;

最佳答案

你要找的是

   return <Grid container justify="center" spacing={2}>
    {/* All images */}
    {docs && docs
        // For every image
        .map(image => (
            // In a grid item
            <Grid className="img-item" item key={image.id} xs={12} md={6} lg={4}>
                {/* all accounts */}
                {docs2 && docs2
                    // For every single image:
                    // Filter statament only contains the user of specific image
                    // https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d
                    .filter((user) => image.userID === user.userID)
                    //Now you have the user that belongs to the image.ID
                    .map(user => (
                        <div key={image.id}>
                            <img src={image.url} alt="uploaded pic" />
                            <Typography variant="subtitle1"> By {user.userName}

                                {/* How do I implement this if statement for the IconButton? */}
                                {/* if ({handleButton(image.userID)} === false){
                                    return 
                                } */}

                                {/* Delete button */}
                                { handleButton(image.userID) && 
                                    <IconButton
                                        disabled={handleButton(image.userID)}
                                        color="secondary" aria-label="delete image"
                                        onClick={() => handleDeleteImage(image.id, image.userID, image.name)}
                                        component="span" >
                                        <DeleteForever />
                                    </IconButton>
                                 }
                            </Typography>
                        </div>
                    ))}
            </Grid>
        ))}
</Grid>
}
export default ImageGrid;

关于javascript - .map() 中的 If 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65828222/

相关文章:

javascript - 为什么内联 JS block 不安全?

javascript - 无法从 React 外部在 ReactJS 组件上设置 State

javascript - Reactjs在功能组件中调用组件后更新状态

javascript - ajax 处理空结果,JQuery

javascript - 由于特殊性,使用 jQuery 添加 'active' 类不起作用

javascript - OpenLayers 可移动标记

javascript - 你如何在 React 中换出一个组件?

reactjs - 在react和typescript中解构useState中的状态

javascript - Apollo 查询返回错误 "Expected String, found String"

javascript - 在 REACT 中构建 GridView