DEV Community

Discussion on: Best approach for filter data, fetch again, array.filter?

Collapse
 
harrison_codes profile image
Harrison Reid

Personally I'd say that what you're doing right now is the best approach to take - keeping the filter logic on the server/API side as much as possible will vastly simplify your client side code. However there are a few techniques you could use to minimise the number of requests you're making to the API...

The simplest would be to add an "Apply Filters" button, so you only update the filters and request new data when the user explicitly requests it (rather than on every change to the input/select fields).

If you're not a fan of that user experience then you could look into using throttle/debounce techniques, which allow you to control the rate at which a function is invoked. For instance, you could throttle the function that makes the API requests so it will only be invoked every 5 seconds, which could prevent multiple requests going out to the API while a user types into the name input.

Collapse
 
joseluisrnp profile image
José Luis Recio

That was a perfect response! I haven't heard about throttle and debounce but after reading the article it makes perfect sense on my mind. Thank you Harrison!

Collapse
 
harrison_codes profile image
Harrison Reid

You're welcome! 😊