Home > Back-end >  React JS - Best way to have coninues results for every key stroke using a REST calls to server?
React JS - Best way to have coninues results for every key stroke using a REST calls to server?

Time:03-13

In short we have a massive database and need to provide results as the user types them in the search box. It is impossible for us to preload queries and attempt to match that way.

Currently we send a request to the server with the new query string every 2 chars or 5 seconds. This is a bit of a mess however, so I'm looking to see if there is a faster/better way of doing this.

Previous solutions I've seen would require pre-feteching which in our case is not possible and considering the size of the return too costly.

CodePudding user response:

I would recommend using debounce for this. It will make the function wait a certain amount of time after being called before running. Additional calls to the function will reset the timer. That way, the function will not run until users have finished (or paused) typing.

This which will prevent unnecessary load on your database, while still providing a good user experience (as long as you have a reasonable debounce time). Examples of how to do debounce in React can be found here

  • Related