State Management with React Query

Book description

Solve some of the most common challenges related to data fetching and data mutations while learning one of the most popular libraries for managing server state in React

Key Features

  • Learn how state is split into server and client state and the common challenges with each
  • Understand how React Query addresses server state challenges by facilitating data fetching and mutations
  • Apply the knowledge gained to improve developer experience and build applications with improved UX

Book Description

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.

What you will learn

  • Learn about state and how it s often managed
  • Discover how state splits into server state and Client state
  • Solve common challenges with server state using React Query
  • Install and configure React Query and its Devtools
  • Manage server state data fetching by using the useQuery hook
  • Create, update and delete data by using the useMutation hook
  • Discover the use of React Query with frameworks like Next.js and Remix
  • Explore MSW and the Testing Library to test React Query using hooks

Who this book is for

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.

Table of contents

  1. State Management with React Query
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
    9. Download a free PDF copy of this book
  7. Part 1: Understanding State and Getting to Know React Query
  8. Chapter 1: What Is State and How Do We Manage It?
    1. Technical requirements
    2. What is state in React?
    3. Managing state in React
      1. Managing state with useState
      2. Managing state with useReducer
      3. Sharing state with React Context
    4. What do different state management libraries have in common?
    5. Summary
  9. Chapter 2: Server State versus Client State
    1. Technical requirements
    2. What is global state?
    3. What is client state?
    4. What is server state?
    5. Understanding common challenges with server state
      1. Caching
      2. Optimistic updates
      3. Deduping requests
      4. Performance optimization
    6. Summary
  10. Chapter 3: React Query – Introducing, Installing, and Configuring It
    1. Technical requirements
    2. What is React Query?
      1. Query
      2. Mutation
      3. How does React Query solve my server state challenges?
    3. Installing React Query
      1. npm
      2. Yarn
      3. pnpm
      4. Script tag
    4. Configuring React Query
      1. QueryClient
      2. QueryClientProvider
    5. Adding React Query Devtools
      1. Floating Mode
      2. Embedded Mode
      3. Enabling Devtools in your production build
    6. Summary
  11. Part 2: Managing Server State with React Query
  12. Chapter 4: Fetching Data with React Query
    1. Technical requirements
    2. What is useQuery and how does it work?
      1. What is a query key?
      2. What is a query function?
      3. What does useQuery return?
      4. Commonly used options explained
    3. Refetching data with useQuery
      1. Automatic refetching
      2. Manual refetching
    4. Fetching dependent queries with useQuery
    5. Putting it all into practice
    6. Summary
  13. Chapter 5: More Data-Fetching Challenges
    1. Technical requirements
    2. Building parallel queries
      1. Manual parallel queries
      2. Dynamic parallel queries
    3. Leveraging QueryClient
      1. Query invalidation
      2. Prefetching
      3. Query cancelation
    4. Creating paginated queries
    5. Creating infinite queries
    6. Debugging your queries with Devtools
    7. Summary
  14. Chapter 6: Performing Data Mutations with React Query
    1. Technical requirements
    2. What is useMutation and how does it work?
      1. What is the mutation function?
      2. What does useMutation return?
      3. Commonly used mutation options explained
    3. Performing side-effect patterns after mutations
      1. How to perform an additional side effect
      2. How to retrigger a query refetch after mutation
      3. How to perform an update to our query data after a mutation
    4. Performing optimistic updates
    5. Summary
  15. Chapter 7: Server-Side Rendering with Next.js or Remix
    1. Technical requirements
    2. Why should I use React Query with server-side rendering frameworks?
    3. Using the initialData pattern
      1. Applying the initialData pattern in Next.js
      2. Applying the initialData pattern in Remix
    4. Using the hydrate pattern
      1. Applying the hydrate pattern in Next.js
      2. Applying the hydrate pattern in Remix
    5. Summary
  16. Chapter 8: Testing React Query Hooks and Components
    1. Technical requirements
    2. Configuring Mock Service Worker
    3. Organizing code
      1. Creating an API file
      2. Leveraging query key factories
      3. Creating a hooks folder
    4. Testing components that use React Query
      1. Setting up testing utils
      2. Testing queries
      3. Testing mutations
    5. Testing custom hooks that use React Query
    6. Summary
  17. Chapter 9: What Changes in React Query v5?
    1. Technical requirements
    2. What are the support changes?
    3. Using only the object format
    4. Removing the logger
    5. Renaming loading to pending
    6. Renaming cacheTime to gcTime
    7. Renaming Hydrate to HydrationBoundary
      1. Next.js hydrate pattern renaming
      2. Remix hydrate pattern changes
    8. Removing keepPreviousData and using placeholderData
    9. Introducing a new way to perform optimistic updates
    10. Introducing maxPages to infinite queries
    11. Summary
  18. Index
    1. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: State Management with React Query
  • Author(s): Daniel Afonso
  • Release date: May 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781803231341