PRE-RELEASE REVIEW COPY // UNLISTED // FOR DATASTAX REVIEW ONLY

6 Tricks for Next-Level CRUDs

Cache more, fetch less with React Query and Axios

Joey Anuff
3 min readOct 30, 2020

Tools: React-Query, Next.js 10, DataStax Document API

To follow along with this demo, initialize a free Cassandra database at DataStax, whose ultra-simple new Documents API we’ll be using for our backend. Make note of your username, password, cluster or database ID, and region. Init a new Next.js app and install these extra dependencies:

npm install axios axios-auth-refresh react-query react-query-devtools

I was looking for a clean, reusable React hooks-based approach for all my endpoints, something lighter than the full Apollo Client suite–maybe just a pattern or two, a minimalist-viable approach to use on all my JSON traffic.

1. CACHE YOUR DATA WITH A REVALIDATION LIBRARY

React Query more than fit the bill: a low-calorie request wrapper that follows the “stale-while-revalidate” approach to data fetching. Vercel’s similar SWR hook library also looked like a good option–Vercel being the creators of this demo’s Next.js framework–but React Query author Tanner Linsley’s over-the-top docs and devtools made his library an almost irresistible choice.

Linsley even threw in a helpful Comparison Table

2. SET YOUR DEFAULTS WITH AXIOS

Different APIs are finicky about different headers. Any day you can DRY up your boilerplate with some global defaults is a guaranteed good day.

Those are my variables, get yours at DataStax.

3. REFRESH TOKENS WITH AXIOS-AUTH-REFRESH

Authentication panic is a common affliction, what’s really “refreshing” is steeping out of the shadows and accepting help, as I did when I installed this mind-relaxing token authenticator.

Axios-auth-refresh boilerplate, except for ‘X-Cassandra-Token’ and the pause config.
The token in local storage is just for debugging.

4. PRE-LOAD YOUR QUERY CACHE (WITHOUT PRE-FETCHING)

This is probably my favorite React Query trick: instead of requesting data for every detail view, React Query builds a cache from your first paginated GET.

Load up the cache from your app’s first GET

5. EXPORT SOME CRUD HOOKS

All our CRUD operations are now simple and exportable. React-Query lets us specify how we want our cache to act during and after each query or mutation.

react-query’s useQuery and useMutation hooks

6. SYNC EVERYTHING WITH REACT-QUERY HOOKS

Our Items index now runs with just two hooks, an items query hook and an addItem mutation hook. Each comes with a full menu of lifecycle booleans.

The items hook fetches the data and sets the cache. The addItem hook invalidates them.

Changes on the detail page are reflected instantly–and reverted if they fail–using the Optimistic Update strategy.

The saveItem hook on each item’s dynamic route page uses Optimistic Updates.

With React Query’s devtools drawer open, we get perfect visibility into the state of our cache.

Unlisted

--

--

Joey Anuff
Joey Anuff

Written by Joey Anuff

ML Developer, comic book stacker

No responses yet