DEV Community

Ridho Putra
Ridho Putra

Posted on

4 2

[ASK] Use useMemo for conditional render

Hi, I quite often use this syntax to render my components conditionally and I've got a lot of review about this to not use useMemo to render markups. Is it wrong doing it this way?

The reason I'm doing this is because of the markups I want to render conditionally is wrapped, I don't want to use the same wrapper in three different places. Any suggestion?

function CardContainer({ data, isLoading }) {
  const renderCards = useMemo(() => {
    if (isLoading) {
      return <Loading />
    }

    if (!data.length) {
      return <EmptyState />
    }

    return <Cards data={data} />
  }, [data, isLoading])

  return <Container>
    {renderCards}
  </Container>
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay