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

284
Visualizações
Getting .startsWith() is not a function in react native

I want to search items through a search bar.The original items are in prod.but I keep getting startsWith() not a function and sometime .toLowerCase() is not a function

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 Respostas
Responde à pergunta

0

I agree with @lucasvw that more information would be helpful here.

However, your conditional is only validating that text!="", but if text is something else, like a number, function, or (more likely) undefined or null, this will throw.

I'd recommend changing to something like this:

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

EDIT:

Sorry, it's actually item that's throwing.

Same idea, but try this instead:

    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 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