Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
React: How to render a fallback for a TypeAhead component when search yields no match?

I working on a Typeahead component in react.

Presently I am just filtering through the data via an text input based and then mapping over it to display filtered options to the DOM. That is coming along but i'd like to display "No items found" if the search yields nothing.

The following is how I am returning the <li></li> items:

   <ul className="search-container__dropdown" onClick={(e) => e.stopPropagation()} style={{ display: show ? 'block' : 'none' }}>
      {comments.length > 0 ? comments
        .filter(({ body, name }) => {
          if (!input) return true;
          if (body.includes(input) || name.includes(input)) {
            return true;
          }
          return false
        })
        .map(({ id, name, body, notfound }, index, arr) => {
          return (
            <li onClick={() => handleClick(id)} className={`search-container__option ${id === cur_section ? "active" : ""}`}
              key={id}
              value={name}>
              <strong>name: </strong>{name},<br />
              <strong>body: </strong>{body}
            </li>
          )
        }) : <NothingFound />}
    </ul>

This is the NothingFound component:

  function NothingFound() {
    return <li>Nothing found</li>
  }

What am I missing?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to filter first and then check the length of the filtered comments like so:

 const filteredComments = comments
  .filter(({ body, name }) => {
    if (!input) return true;
    if (body.includes(input) || name.includes(input)) {
      return true;
    }
    return false;
 })
  
   return (<main>
          ...
          {filteredComments.length > 0 ? (
            filteredComments
              .map(({ id, name, body, notfound }, index, arr) => {
           return (
              <li ...
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda