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

282
Views
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 answers
Answer question

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