
Kara Sune - January 29, 2024
+4
When at first I started building React apps back in then, Axios was the first tool I used for handling API requests. It made things easy, just send a request, get the response, and you’re done. But as my apps got bigger, I realized that fetching data wasn’t the hard part.
The real challenge was managing state, handling errors, and deciding when to refetch data. I found myself writing the same logic over and over again. That’s when I discovered React Query, and everything changed.
With Axios, I always had to set up loading and error states manually and this works, but it’s repetitive. Every time I made a request, I had to write this same pattern.
But with React Query, less stress, less boilerplate.
React Query takes care of the loading, error, and success states automatically and even to beyond of what you are expecting.
One thing I struggled with was caching. Let’s say I load a list of courses. If I navigate to another page and come back, Axios would fetch the data again, even if nothing changed.
React Query fixes this. It caches the data, so if I return to the page, the cached data shows up instantly while it quietly checks for updates in the background. This makes the app feel much faster, and I didn’t have to write any extra code to make it happen.
While React Query and Axios are great, there are other libraries worth exploring, depending on your project needs as well.
Each tool right here has its strengths, so it’s worth experimenting to see what works best for your stack. For me, React Query feels like a game-changer, but exploring different options helped me understand what I really needed, so i believe you will want to check it out. Try them out, and you might find the perfect fit for your next project!
The main reason I switched to React Query is simple. It saves me time and reduces the amount of code I need to write. I can focus on building features instead of constantly worrying about how to manage API requests. I still use Axios for the actual requests because it’s great at that. But wrapping it in React Query makes my life so much easier.
If you’re still handling API calls manually, give React Query a try.
It might feel a bit different at first, but once you see how much it simplifies things, you’ll probably never want to go back.
Thanks for reading.