Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

does-this-replace-client-state.md

56 lines2.4 KB

1---
2id: does-this-replace-client-state
3title: Does TanStack Query replace Redux, MobX or other global state managers?
4---
5​
6Well, let's start with a few important items:
7​
8- TanStack Query is a **server-state** library, responsible for managing asynchronous operations between your server and client
9- Redux, MobX, Zustand, etc. are **client-state** libraries that _can be used to store asynchronous data, albeit inefficiently when compared to a tool like TanStack Query_
10​
11With those points in mind, the short answer is that TanStack Query **replaces the boilerplate code and related wiring used to manage cache data in your client-state and replaces it with just a few lines of code.**
12​
13For a vast majority of applications, the truly **globally accessible client state** that is left over after migrating all of your async code to TanStack Query is usually very tiny.
14​
15> There are still some circumstances where an application might indeed have a massive amount of synchronous client-only state (like a visual designer or music production application), in which case, you will probably still want a client state manager. In this situation it's important to note that **TanStack Query is not a replacement for local/client state management**. However, you can use TanStack Query alongside most client state managers with zero issues.
16​
17## A Contrived Example
18​
19Here we have some "global" state being managed by a global state library:
20​
21```tsx
22const globalState = {
23 projects,
24 teams,
25 tasks,
26 users,
27 themeMode,
28 sidebarStatus,
30```
31​
32Currently, the global state manager is caching 4 types of server-state: `projects`, `teams`, `tasks`, and `users`. If we were to move these server-state assets to TanStack Query, our remaining global state would look more like this:
33​
34```tsx
35const globalState = {
36 themeMode,
37 sidebarStatus,
39```
40​
41This also means that with a few hook calls to `useQuery` and `useMutation`, we also get to remove any boilerplate code that was used to manage our server state e.g.
42​
43- Connectors
44- Action Creators
45- Middlewares
46- Reducers
47- Loading/Error/Result states
48- Contexts
49​
50With all of those things removed, you may ask yourself, **"Is it worth it to keep using our client state manager for this tiny global state?"**
51​
52**And that's up to you!**
53​
54But TanStack Query's role is clear. It removes asynchronous wiring and boilerplate from your application and replaces it with just a few lines of code.
55​
56What are you waiting for, give it a go already!