英文:
react-packed-grid: null child is inserting a grid item
问题
我有一个问题,即使不应显示任何内容,我的数据仍然返回一个空的 div,这导致我的设计出现问题。
上面的情况应该在网格中间返回项目 5,但实际上它在左侧,这是因为remainingItemsCount
仍然在其右侧返回一个空的 div。您可以检查HTML以查看它。
我编写了一个应该保持为空的条件,但它不起作用。我相信问题出在我正在使用的软件包中,但我认为必须有一些解决方案。
这是我的代码示例: https://codesandbox.io/s/grid-16-9-ratio-forked-5qt1rx?file=/src/App.js
英文:
I have a problem that my data is returning an empty div even if it shouldn't display anything, which causes my design to be broken.
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const itemsToDisplay = data.slice(0, 5);
const remainingItemsCount = data.length - itemsToDisplay.length;
<PackedGrid boxAspectRatio={aspectRatio} className="fullscreen">
{itemsToDisplay.map((i) => (
<GridItemPlaceholder>{i}</GridItemPlaceholder>
))}
{remainingItemsCount > 20 ? (
<GridItemPlaceholder>+{remainingItemsCount}</GridItemPlaceholder>
) : null}
</PackedGrid>`
The case above should return item 5 in the middle of the grid, but instead it's on left side and that's because remainingItemsCount
is still returning an empty div on the right side of it. You can inspect the HTML to see it.
I wrote a condition that should stay null, but it does not work. I believe the issue is in the package I am using, but I figure there has to be some solution.
Here is my code example: https://codesandbox.io/s/grid-16-9-ratio-forked-5qt1rx?file=/src/App.js
答案1
得分: 0
这对您有帮助
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const itemsToDisplay = data.slice(0, 5);
const remainingItemsCount = data.length - itemsToDisplay.length;
if (remainingItemsCount > 20) {
itemsToDisplay.push(`+${remainingItemsCount}`)
}
return (
<>
<PackedGrid boxAspectRatio={aspectRatio} className="fullscreen">
{itemsToDisplay.map((i) => (
<GridItemPlaceholder>{i}</GridItemPlaceholder>
))}
</PackedGrid>
</>
);
英文:
This does the trick for you
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const itemsToDisplay = data.slice(0, 5);
const remainingItemsCount = data.length - itemsToDisplay.length;
if (remainingItemsCount > 20) {
itemsToDisplay.push(`+${remainingItemsCount}`)
}
return (
<>
<PackedGrid boxAspectRatio={aspectRatio} className="fullscreen">
{itemsToDisplay.map((i) => (
<GridItemPlaceholder>{i}</GridItemPlaceholder>
))}
</PackedGrid>
</>
);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论