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

119
Vistas
I am trying to add add Search filter in react but I am getting this error
  1. I am trying to add Seach filter using the react, and using json data I am trying to match it with the search term

  2. Below is my code

    const App = () => { const [searchTerm, setSearchTerm] = useState([""]) const [query, setQuery] = useState("");

    useEffect(() => { const url = "https://60d075407de0b20017108b89.mockapi.io/api/v1/animals";

    const fetchData = async () =>
      {
              try
          {
              const response = await fetch(url);
              const json = await response.json();
              console.log([json].query);
              setQuery(json.query);
          }
              catch (error)
          {
              console.log("error", error);
          }
      };
      fetchData();
    

    }, []);

    return ( <input type='text' placeholder='search....' onChange={event => { setSearchTerm(event.target.value) }} />

      {
    
              query.filter((val) => {
                if (searchTerm === "s")
          {
              return val
          }
              else if (val.name.toLowerCase().includes(searchTerm.toLowerCase())) {
              return val
          }
          else
              return false
          }).map((val) =>
          {
              return (
              <div className='user' >
              <p>{val.name}</p>
              <p>age: {monthDiff(val.bornAt)} months</p>
    
            </div>
          );
          })}
    </div>
    

    ); };

When I try to execute, I am getting this below error can anyone explain why it is happening

> Uncaught TypeError: Cannot read properties of undefined (reading
> 'toLowerCase')
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It looks like you're initializing query to a string instead of an array of strings.

Maybe try changing this:

const [query, setQuery] = useState("");

to

const [query, setQuery] = useState([""]);

Also, your searchTerm is initialized to an array: you might have just mixed those up :)

about 4 years ago · Santiago Trujillo 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