Back

Notes

Tanstack Query

Tanstack Query is state-management library meant to sit cozily between your network and app / UI layers.

What is a Query™?

A function that returns data, and a key under which that data is stored and updated.

Queries can become
Stale
after a certain amount of time has passed. Different behavior can be configured to handle stale data.

What is a Query Invalidation?

Invalidation means marking the data under a query key
Stale

What does it mean for data to
Stale
?

This just means that a certain amount of time has passed since the data was last
fetched
or
refetched
.

What is
Fetching
?

Getting data for the first time.

What is
Refetching
?

Getting data after at least one
Fetch

When does
Refetching
occur?

  • 1. When triggered manually.
  • 2. When it's configured to run on
    Stale
    data and a query is invalidated.

What are Query Keys

A list of strings used as a hashmap key to store data for a given query. Later, these keys are used for managing the cache and invalidating query data.

How to Query Keys?

1. Inline them 🙈

I mean what's so bad about magic strings, really?

2. Use a library

The query key factory community package is endorsed by the official tanstack query site.

3. Use Key Factories

Follow the official tanstack query docs