Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
How to display a message inside an empty div when a component doesn't render in it?

I have a div inside which I'm rendering dynamic card components(SearchRes):

<div className="search-res">
            {games.filter(game => (game.background_image !== null)&&(game.ratings_count>38)).map(game=>
            <SearchRes
            key={game.name} 
            title={game.name} 
            rating={game.metacritic===null?<MdGamepad/>:game.metacritic} 
            genre={game.genres.map(genre=>genre.name+" ")} 
            imglnk={(game.background_image===null)?"":game.background_image}
            gameid={game.id}
            slug={game.slug}
            plat={game.parent_platforms}
            />
            )}
 </div>

When the cards don't render (when they don't pass the filter conditions), I get a blank space. I want to display something like no results found when such thing happens, how can I implement this? Currently I'm using CSS to implement it:

.search-res:empty::before {
content: "No data";}

but clearly this is not viable as the message is visible when the search is not even initiated: message displayed when search isn't even initiated

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If I understand your question, you want to conditionally render a "No Data" fallback only after you've fetched data and there is nothing to render.

  1. Add a loading state and conditionally render a loading indicator.

  2. Filter the games and in the render check that there is an array length (i.e. something to map), and conditionally render the array or the fallback text if there is a current search term.

Code

const [searchLoaded, setSearchLoaded] = React.useState(false);

// in search handler
// toggle searchLoaded false when initiating a search
// toggle searchLoaded true when search completes

const results = games.filter(
  game => game.background_image !== null && game.ratings_count > 38
);

<div className="search-res">
  {results.length
    ? results.map(game => (
      <SearchRes
        key={game.name} 
        title={game.name} 
        rating={game.metacritic === null ? <MdGamepad/> : game.metacritic} 
        genre={game.genres.map(({ name }) => name).join(" ")} 
        imglnk={game.background_image === null ? "" : game.background_image}
        gameid={game.id}
        slug={game.slug}
        plat={game.parent_platforms}
      />
    ))
    : searchLoaded && "No Data"
  }
</div>
about 4 years ago · Juan Pablo Isaza Relatório

0

I would suggest to filter the array first, based on the conditions and then check if the array has length i.e. arr.length !== 0 and if that is the case, then map the results of the array to the card component. And if not then show the message that no data found. You would have to use if/else or ternary operator.

Thanks😊

about 4 years ago · Juan Pablo Isaza Relatório

0

if you are using a filter only for conditioning then there is no need for a filter instead you can use conditional rendering like this:

<div className="search-res">
            {games.map(game=>
            {(game.background_image !== null && game.ratings_count>38) ?
            <SearchRes
            key={game.name} 
            title={game.name} 
            rating={game.metacritic===null?<MdGamepad/>:game.metacritic} 
            genre={game.genres.map(genre=>genre.name+" ")} 
            imglnk={(game.background_image===null)?"":game.background_image}
            gameid={game.id}
            slug={game.slug}
            plat={game.parent_platforms}
            /> : "NO RESULTS FOUND"}
            )}
 </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda