Skip to content
Advertisement

how to implement search API in react native

I have Text Input like this

JavaScript

Now I have func like this

JavaScript

this is my API calling code

JavaScript

Now my question is Result is searing and showing correctly as I started typing in text box. But I remove all text from text input by back pressing keyboard button. then It should show all result. But it only showing That keyword which I remove recently by back press keyboard button. please help thanks.

Advertisement

Answer

While the user typing, searchAPI receives old input value due to how to React state update and component rendering works.

Try this refactor.

JavaScript

Then searchAPI

JavaScript

You can improve user experience and save server resources by debouncing search queries and only querying the database when the user finishes typing the search query.

JavaScript
Advertisement