react-packed-grid: null child is inserting a grid item

huangapple go评论54阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2023年2月9日 02:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定