Cover Page....2
Table of Contents....3
Preface....4
Part 1: Understanding State and Getting to Know React Query....13
Chapter 1: What Is State and How Do We Manage It?....14
Technical requirements....14
What is state in React?....15
Managing state in React....17
What do different state management libraries have in common?....24
Summary....25
Chapter 2: Server State versus Client State....26
Technical requirements....27
What is global state?....27
What is client state?....31
What is server state?....32
Understanding common challenges with server state....34
Summary....38
Chapter 3: React Query – Introducing, Installing, and Configuring It....39
Technical requirements....39
What is React Query?....40
Installing React Query....43
Configuring React Query....45
Adding React Query Devtools....51
Summary....58
Part 2: Managing Server State with React Query....59
Chapter 4: Fetching Data with React Query....60
Technical requirements....60
What is useQuery and how does it work?....61
Refetching data with useQuery....81
Fetching dependent queries with useQuery....85
Putting it all into practice....87
Summary....94
Chapter 5: More Data-Fetching Challenges....96
Technical requirements....96
Building parallel queries....97
Leveraging QueryClient....100
Creating paginated queries....111
Creating infinite queries....115
Debugging your queries with Devtools....119
Summary....124
Chapter 6: Performing Data Mutations with React Query....126
Technical requirements....127
What is useMutation and how does it work?....127
Performing side-effect patterns after mutations....148
Performing optimistic updates....153
Summary....158
Chapter 7: Server-Side Rendering with Next.js or Remix....159
Technical requirements....159
Why should I use React Query with server-side rendering frameworks?....160
Using the initialData pattern....161
Using the hydrate pattern....165
Summary....172
Chapter 8: Testing React Query Hooks and Components....174
Technical requirements....174
Configuring Mock Service Worker....175
Organizing code....178
Testing components that use React Query....183
Testing custom hooks that use React Query....210
Summary....219
Chapter 9: What Changes in React Query v5?....221
Technical requirements....222
What are the support changes?....222
Using only the object format....222
Removing the logger....224
Renaming loading to pending....224
Renaming cacheTime to gcTime....225
Renaming Hydrate to HydrationBoundary....226
Removing keepPreviousData and using placeholderData....228
Introducing a new way to perform optimistic updates....229
Introducing maxPages to infinite queries....234
Summary....235
Index....237
Why subscribe?....250
Other Books You May Enjoy....252
Packt is searching for authors like you....255
Share your thoughts....255
Download a free PDF copy of this book....256
Beat common challenges in data fetching and data mutations while learning one of the most popular libraries for managing server state in React
Purchase of the print or Kindle book includes a free PDF eBook
State management, a crucial aspect of the React ecosystem, has gained significant attention in recent times. While React offers various libraries and tools to handle state, each with different approaches and perspectives, one thing is clear: state management solutions for handling client state are not optimized for dealing with server state. React Query was created to address this issue of managing your server state, and this guide will equip you with the knowledge and skills needed to effectively use React Query for state management.
Starting with a brief history of state management in the React ecosystem, you'll find out what prompted the split from a global state to client and server state and thus understand the need for React Query. As you progress through the chapters, you'll see how React Query enables you to perform server state tasks such as fetching, caching, updating, and synchronizing your data with the server. But that's not all; once you've mastered React Query, you'll be able to apply this knowledge to handle server state with server-side rendering frameworks as well. You'll also work with patterns to test your code by leveraging the testing library and Mock Service Worker.
By the end of this book, you'll have gained a new perspective of state and be able to leverage React Query to overcome the obstacles associated with server state.
The book is for React developers who want to improve their state management skills and overcome the hurdles brought about by server state while improving their developer and user experience. Basic knowledge of web development and React will assist with understanding key concepts covered in this book. JavaScript developers will also find it useful.