Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Obtener .startsWith() no es una función en reaccionar nativo

Quiero buscar elementos a través de una barra de búsqueda. Los elementos originales están en producción.

 const [prod, setprod] = React.useState([]); const [getFiltered, setFiltered] = React.useState([]); const [getSearch,setSearch]=React.useState(""); const SearchItem=(text)=>{ if(text!=""){ const searched=prod.filter((item)=>{ const datachange=item.toLowerCase(); const textchange= text.toLowerCase(); return datachange.startsWith(textchange); }); setFiltered(searched); setSearch(text) } else{ setFiltered(prod); setSearch(text); }}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Estoy de acuerdo con @lucasvw en que sería útil más información aquí.

Sin embargo, su condicional solo está validando ese text!="" , pero si el texto es otra cosa, como un número, una función o (más probablemente) undefined o null , esto arrojará.

Recomiendo cambiar a algo como esto:

 if (typeof text === "string" && text != "") { # the rest of your code }

EDITAR:

Lo sentimos, en realidad es el item que está lanzando.

La misma idea, pero prueba esto en su lugar:

 const searched=prod.filter((item)=>{ if (typeof item !== "string") { return false; } const datachange=item.toLowerCase(); const textchange= text.toLowerCase(); return datachange.startsWith(textchange); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!