Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning null when condition is met does not work

I have a problem that my data is returning empty div even if it shouldn’t display anything – causing 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>`

Case above should return item 5 in the middle of the grid, but instead is on left side and that’s because remainingItemsCount are still returning an empty div on the right side of it – you can inspect 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 there has to be some solution?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is code example

https://codesandbox.io/s/grid-16-9-ratio-forked-5qt1rx?file=/src/App.js

>Solution :

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>
    </>
  );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading