DEV Community

Cover image for Modernizing Data Fetching in React
NK PESWANI
NK PESWANI

Posted on

Modernizing Data Fetching in React

In the ever-evolving world of web development, staying up-to-date with the latest tools and techniques is essential to building efficient and performant applications. One critical aspect of any web application is data fetching, which is often a complex and crucial task. In the past, developers commonly relied on libraries like Axios and the native fetch API to handle data retrieval, but as we navigate the dynamic landscape of front-end development, new and more powerful solutions have emerged.

This blog post explores the shift from traditional data fetching methods, like Axios and Fetch, to a modern and developer-friendly alternative: SWR, also known as Stale-While-Revalidate, and its integration with React Query. With SWR and React Query, developers can simplify data management, reduce complexity, and enhance the overall performance of their React applications.

So, why the switch? How does SWR (React Query) revolutionize data fetching in React, and what makes it a game-changer for developers? Join us on this journey as we dive into the world of efficient data fetching and learn how to harness the capabilities of SWR to create smoother, more responsive, and maintainable applications. Whether you're a seasoned developer or just starting your web development journey, this transition will prove to be a leap forward in your React development skills. Let's embark on this journey to unlock the potential of SWR (React Query) and usher in a new era of data fetching in your React applications.

The Old Way - Axios and Fetch

For years, web developers have relied on two primary methods to fetch data from APIs or servers: Axios and the native fetch API. These methods served us well and still have their place, but they come with their fair share of limitations.

Axios - A Popular Choice

Axios, a JavaScript library for making HTTP requests, quickly gained popularity due to its simplicity and consistent API. Developers appreciated its clean syntax, promise-based structure, and the ability to handle complex request configurations. With Axios, making GET, POST, PUT, and DELETE requests became straightforward. It was the go-to choice for many developers for quite some time.

Native fetch - Simplicity and Widespread Usage

On the other hand, the native fetch API provided a built-in way to make network requests in modern JavaScript. It was lauded for its simplicity and the fact that it didn’t require additional dependencies. Its adoption was also encouraged by its broad support in modern browsers.

Challenges and Limitations

While Axios and fetch were reliable for basic data fetching, they presented challenges as applications grew in complexity:

  • Caching and Revalidation: Maintaining a proper cache and implementing revalidation strategies was often a manual and complex task. Developers had to handle caching of responses, triggering re-fetching when data became stale, and ensuring consistency across different parts of their applications.

  • Reactivity: Achieving real-time reactivity when data changed on the server required significant effort. This typically involved creating custom solutions using tools like Redux or Context API.

  • Optimistic Updates: Performing optimistic updates, where UI changes occur before the server confirms the action’s success, was challenging with Axios and fetch. Developers had to manually manage these scenarios, often resulting in complex code.

  • Error Handling: Error handling, such as retries and showing error messages to users, was often inconsistent across applications and required extra work.

In summary, while Axios and fetch were sufficient for basic data fetching, they posed hurdles as applications became more complex. These challenges led to the need for a modern solution that could simplify data fetching and management, which brings us to SWR (Stale-While-Revalidate) and React Query. In the next sections, we'll explore how these tools are changing the game for React developers, making data fetching more efficient and developer-friendly.

Enter SWR (React Query)

In the ever-advancing world of web development, solutions often emerge to address the shortcomings of existing tools. One such solution that’s been gaining traction in recent years is SWR, short for "Stale-While-Revalidate." In conjunction with React Query, SWR is revolutionizing the way we approach data fetching in React applications.

What is SWR?

At its core, SWR is a JavaScript library designed to simplify data fetching in a React application. Its name, "Stale-While-Revalidate," encapsulates its primary purpose. SWR leverages the concept of caching to improve the performance and efficiency of data fetching.

Here’s how SWR works:

  • Stale: When you first request data, SWR provides you with the cached data if available, even if it’s considered "stale." Stale data is still useful and can be displayed immediately to the user. This means your application doesn’t need to wait for fresh data to render something meaningful.
  • While: While displaying the stale data, SWR then sends a background request to the server to fetch the latest data.
  • Revalidate: Once the new data is received, SWR updates the cache and re-renders your component with the fresh data.
  • The beauty of SWR lies in its simplicity. It automatically manages these caching, revalidation, and re-rendering processes, ensuring that your application remains responsive and up-to-date without extra coding effort.

React Query: A Perfect Partner

While SWR handles the data fetching and caching aspects elegantly, it pairs exceptionally well with React Query. React Query is a library specifically designed for data synchronization and state management in React applications. When used in tandem with SWR, React Query takes care of state management, mutation handling, and data synchronization, providing a comprehensive solution for your data management needs.

Together, SWR and React Query offer the following benefits:

  • Simplicity: These libraries are known for their developer-friendly APIs, reducing the complexity of data management and allowing you to focus on building features.
  • Efficiency: SWR’s caching and React Query’s state management result in more efficient data fetching, eliminating unnecessary network requests and re-renders.
  • Reactivity: Achieving real-time reactivity is straightforward. Whenever data changes on the server, SWR and React Query update your application automatically.
  • Optimistic Updates: Handling optimistic updates becomes a breeze. You can update your UI optimistically and let the libraries take care of syncing with the server.
  • Error Handling: Error handling, including retries and showing error messages, is streamlined, improving the overall user experience.

In the subsequent sections of this blog post, we’ll delve into practical examples, demonstrating how to set up and use SWR and React Query for efficient data fetching and state management. Say goodbye to the complexities of traditional data fetching with Axios and fetch, and welcome a new era of simplicity and efficiency in React development.

Setting Up SWR

Before you can harness the power of SWR (Stale-While-Revalidate) and React Query for data fetching and state management, you’ll need to set them up in your React application. Let’s go through the steps to get started with SWR:

Installation
The first step is to install the SWR library. You can do this by using a package manager like npm or yarn. Open your terminal and navigate to your project directory, then run:

npm install swr
Enter fullscreen mode Exit fullscreen mode

or

yarn add swr
Enter fullscreen mode Exit fullscreen mode

This will add the SWR library to your project’s dependencies.

Basic Configuration

To get SWR up and running, you’ll need to import it and configure it. In your React component or a separate module, import useSWR from the SWR library:

import useSWR from 'swr'
Enter fullscreen mode Exit fullscreen mode

Creating a Custom Hook

One of the best practices is to create a custom hook for your data fetching needs. This encapsulates the logic and makes it reusable across your application. Here’s an example of a custom hook that uses SWR:

import useSWR from 'swr';

const fetcher = (url) => fetch(url).then((res) => res.json());

export function useData() {
  const apiUrl = '/api/data'; // Replace with your actual API endpoint
  const { data, error } = useSWR(apiUrl, fetcher);

  return {
    data,
    error,
    isLoading: !data && !error,
  };
}
Enter fullscreen mode Exit fullscreen mode

In this example:

We define a fetcher function that fetches data from a given URL and parses it as JSON. You can customize this function based on your data source.
The useData custom hook uses useSWR to fetch data from the specified API endpoint. It returns the data, any potential errors, and a loading indicator.

Using the Custom Hook

Now that you have your custom hook, you can easily use it in your React components. Here’s an example of how to use the useData hook:

import React from 'react';
import { useData } from './your-custom-hook';

function MyComponent() {
  const { data, error, isLoading } = useData();

  if (isLoading) {
    return <div>Loading data...</div>;
  }

  if (error) {
    return <div>Error loading data</div>;
  }

  // Render your component with the fetched data
  return (
    <div>
      <h1>Data from SWR:</h1>
      <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

With these simple steps, you’ve set up SWR in your React application. SWR handles the caching, revalidation, and background updates for you, making data fetching a breeze. In the next section, we’ll explore how to actually fetch data using SWR and handle various scenarios.

Fetching Data with SWR

Now that you’ve set up SWR (Stale-While-Revalidate) in your React application, let’s dive into how to use it to fetch data efficiently. SWR simplifies the process of fetching and managing data, making it an excellent choice for modern React applications.

Data Fetching with SWR

Fetching data with SWR is remarkably straightforward. We’ll use the custom hook we created earlier. Here’s a recap of the custom hook:

import useSWR from 'swr';

const fetcher = (url) => fetch(url).then((res) => res.json());

export function useData() {
  const apiUrl = '/api/data'; // Replace with your actual API endpoint
  const { data, error } = useSWR(apiUrl, fetcher);

  return {
    data,
    error,
    isLoading: !data && !error,
  };
}
Enter fullscreen mode Exit fullscreen mode

Handling Loading and Errors

When you use the useData custom hook, it returns three values: data, error, and isLoading. Here’s how to handle them:

If isLoading is true, you can display a loading message to let the user know that the data is being fetched.
If error is not null, you can display an error message to handle any issues that may have occurred during data retrieval.

if (isLoading) {
  return <div>Loading data...</div>;
}

if (error) {
  return <div>Error loading data</div>;
}
Enter fullscreen mode Exit fullscreen mode

Displaying Data

If neither isLoading nor error is true, you can safely assume that you have successfully fetched the data. You can then render your component with the fetched data

return (
  <div>
    <h1>Data from SWR:</h1>
    <pre>{JSON.stringify(data, null, 2)}</pre>
  </div>
);

Enter fullscreen mode Exit fullscreen mode

Automatic Caching and Revalidation

One of the most significant benefits of SWR is its automatic caching and revalidation behavior. When you first load the data, SWR provides the cached data if available, even if it's considered "stale." It then sends a background request to the server to fetch the latest data. Once the new data is received, SWR updates the cache and re-renders your component with the fresh data.

This ensures that your application remains responsive, and your users get the most up-to-date information without unnecessary network requests.

Automatic Request Deduplication

Another noteworthy feature of SWR is automatic request deduplication. If you have multiple components fetching the same data concurrently, SWR ensures that there's only one network request for that data. This eliminates redundant requests and optimizes network usage.

With SWR, you can see how easy it is to fetch and manage data in your React application. The library takes care of caching, revalidation, and background updates, making your code cleaner and your application more performant. In the next section, we'll explore how to take your data management even further with React Query, as it complements SWR perfectly for a complete data management solution.

Managing State with React Query:

While SWR (Stale-While-Revalidate) is excellent for handling data fetching and caching, it’s just one piece of the puzzle when it comes to efficient data management in React applications. Enter React Query, a library specifically designed for state management and data synchronization. When combined with SWR, React Query creates a comprehensive data management solution that simplifies state handling and offers advanced features for real-time updates and mutations.

What is React Query?

React Query is a powerful library that provides a centralized state management solution for your application’s data. It excels in several areas:

  • State Management: React Query manages the state of your data, including caching, background syncing, and keeping everything in sync across components.

  • Mutations: It simplifies handling data mutations, allowing you to perform optimistic updates and providing tools to ensure the UI reflects the most current data.

  • Background Updates: React Query automatically refreshes your data in the background, ensuring that your application always displays the latest information.

  • Error Handling: It streamlines error handling, including retries and the ability to provide users with clear error messages.

Setting Up React Query

To get started with React Query, you need to install it and configure a QueryClient to manage your application’s state. Here’s how to do it:

Installation

Just like SWR, you can install React Query using npm or yarn. Run the following command in your terminal:

npm install react-query
Enter fullscreen mode Exit fullscreen mode

or

yarn add react-query
Enter fullscreen mode Exit fullscreen mode

Creating a QueryClient

In your application’s entry point (usually the main React component or a separate file), set up a QueryClient. This client will manage your application’s state.

import { QueryClient, QueryClientProvider } from 'react-query';

const queryClient = new QueryClient();

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      {/* Your application components */}
    </QueryClientProvider>
  );
}
Enter fullscreen mode Exit fullscreen mode

Using React Query with SWR

Now that you have React Query set up, you can use it alongside SWR. React Query enhances SWR’s capabilities, providing a full data management solution.

Queries with React Query

In React Query, you define queries that describe the data you want to fetch. A query typically includes:

  • A unique key for the query, which helps React Query identify and cache the data.

  • A fetch function to retrieve the data.

Here’s an example of a query:


import { useQuery } from 'react-query';

const key = 'data'; // A unique key for this query
const fetchFunction = async () => {
  // Fetch your data here
};

const { data, error, isLoading } = useQuery(key, fetchFunction);
Enter fullscreen mode Exit fullscreen mode

Mutations with React Query

React Query makes handling data mutations easy. It provides useMutation for performing updates, deletions, and other mutations. You can specify an optimistic update function to reflect changes in your UI before the mutation is confirmed on the server.

import { useMutation } from 'react-query';

const updateData = async (newData) => {
  // Perform the mutation on the server
};

const { mutate, isLoading } = useMutation(updateData, {
  onMutate: (newData) => {
    // Perform the optimistic update here
  },
});
Enter fullscreen mode Exit fullscreen mode

Real-time Reactivity and Synchronization

React Query and SWR together provide real-time reactivity for your data. Whenever the data changes on the server, these libraries ensure that your application updates automatically. This allows you to build responsive, real-time features without the complexity of managing state manually.

With React Query’s mutations and SWR’s data fetching, you have a powerful set of tools to manage your application’s data efficiently and provide an outstanding user experience. In the next section, we’ll explore practical use cases and real-world examples of how these libraries simplify data management in React applications.

Handling Mutations and Data Updates:

In modern web applications, managing data is not just about fetching information from a server. It’s also about handling mutations—making changes to the data, such as creating, updating, and deleting records. Additionally, ensuring that your application stays in sync with the server, especially when other users make changes, is crucial. SWR (Stale-While-Revalidate) and React Query offer powerful tools to handle mutations and data updates effectively.

Mutations with React Query

React Query simplifies handling mutations in your application. Using useMutation, you can easily define mutation functions and manage the optimistic updates for a responsive user interface.

Here's an example of how to perform a mutation with React Query:

import { useMutation, useQueryClient } from 'react-query';

const updateData = async (newData) => {
  // Perform the mutation on the server
};

function MyComponent() {
  const queryClient = useQueryClient();

  const { mutate, isLoading } = useMutation(updateData, {
    onMutate: (newData) => {
      // Perform the optimistic update
      queryClient.setQueryData('data', (prevData) => {
        return { ...prevData, ...newData };
      });
    },
    // onSuccess, onError, and other callbacks can be defined here
  });

  // Call the mutate function when you want to perform the mutation
}
Enter fullscreen mode Exit fullscreen mode

In this example, we use useMutation to define the mutation function (updateData) and specify an onMutate callback where we perform an optimistic update using setQueryData. This ensures a responsive user interface by immediately reflecting the changes before the mutation is confirmed on the server.

Background Updates with SWR

SWR handles background updates efficiently. When you perform a mutation using React Query, React Query invalidates the affected queries. SWR, in turn, automatically revalidates those queries in the background to ensure that your application remains up-to-date.

Here’s how it works:

  • You perform a mutation using useMutation.

  • React Query invalidates related queries, marking them as "stale."

  • SWR, aware of the "stale" queries, sends background requests to revalidate the data.

  • When the updated data is received, SWR automatically updates the cache and re-renders your components.

This background update process ensures that the data in your application stays in sync with the server without the need for manual intervention.

Handling Data Conflicts

In applications with multiple users, data conflicts can occur when multiple users try to update the same piece of data simultaneously. React Query provides features like "optimistic updates" and "background polling" to help mitigate these conflicts. When conflicts arise, you can define how you want to handle them in your mutation functions.

Real-time Synchronization

Thanks to SWR and React Query’s revalidation and background updates, your application can achieve real-time synchronization with the server. This is vital for building features that require immediate user feedback and ensure that all users see the most up-to-date data.

With SWR and React Query, you have the tools to efficiently handle mutations and data updates in your React application, providing a smooth and responsive user experience. In the next section, we’ll dive into real-world use cases and practical examples of how these libraries simplify data management in various scenarios.

Real-World Use Cases:

To truly understand the value of SWR and React Query, it’s essential to see how these libraries shine in real-world use cases. Let’s explore practical examples of how they simplify data management in various scenarios, from paginated data to forms and authentication.

Paginated Data

  • Scenario: You’re building a news website, and you need to display a list of articles. However, you can’t load all articles at once due to performance and bandwidth constraints, so you paginate the data.

  • Solution: SWR simplifies managing paginated data. It automatically handles caching, revalidation, and background updates. When a user navigates between pages, SWR fetches the required data and ensures the content remains up-to-date. React Query makes pagination even more powerful by managing the state and handling mutations like upvoting or commenting on articles.

Forms and Real-time Updates

  • Scenario: You’re creating a messaging app where users can send messages to each other in real time. You want the user interface to be responsive and show sent messages immediately.

  • Solution: SWR’s caching and background updates ensure that messages stay up-to-date without manual polling. When a user sends a message, SWR revalidates the chat data in the background. React Query, in combination with SWR, makes it easy to handle mutations and optimistic updates, creating a seamless real-time chat experience.

Authentication and User Data

  • Scenario: Your application requires user authentication, and you need to fetch user-specific data after a user logs in.

  • Solution: SWR is great for handling user data because it ensures that user information remains fresh. When a user logs in, SWR automatically revalidates their data, providing an updated user experience. React Query can manage user-related data, such as preferences and settings, and handle mutations like changing a user’s profile information.

E-commerce and Cart Management

  • Scenario: You’re building an e-commerce platform where users can add items to their shopping carts and proceed to checkout.

  • Solution: SWR and React Query are ideal for cart management. SWR handles product information, ensuring that users always see accurate prices and availability. React Query can manage the shopping cart state, allowing you to add and remove items and calculate totals. Optimistic updates and background synchronization ensure a smooth shopping experience.

Data Sync Across Multiple Components

  • Scenario: Your application consists of multiple components that need to share and stay in sync with the same data.

  • Solution: SWR and React Query provide a centralized way to manage data shared across various components. Using a single source of truth, you can ensure that all components access the same data and stay updated when changes occur. This simplifies complex data-sharing scenarios and prevents inconsistencies across your application.

These real-world use cases demonstrate how SWR and React Query simplify data management, ensuring that your application is responsive, efficient, and up-to-date. Whether you’re working with paginated data, real-time messaging, user authentication, e-commerce, or multi-component data synchronization, these libraries provide a powerful and developer-friendly solution for your data management needs. Embrace the future of React data management and experience the benefits of SWR and React Query in your projects.

Conclusion:

Embrace the Future of Data Management in React

In the dynamic world of web development, staying on the cutting edge is essential to building applications that are efficient, responsive, and user-friendly. Data management, a fundamental aspect of any application, has evolved significantly over the years. The transition from traditional methods like Axios and fetch to modern tools like SWR (Stale-While-Revalidate) and React Query is a leap forward in the world of React development.

We’ve embarked on a journey through the intricacies of data management, and we’ve seen how SWR and React Query simplify this vital task. These libraries, although separate in their functionalities, come together as a powerful duo to enhance the developer experience and improve application performance.

  • Efficiency and Simplicity: SWR automates data caching, revalidation, and background updates, reducing complexity and ensuring that your application remains responsive even in the face of slow networks. No more manual caching, revalidation, or polling.
  • State Management: React Query provides centralized state management for your application, handling mutations, data synchronization, and error handling. The combination of SWR and React Query makes complex data management straightforward.
  • Real-World Scenarios: We’ve explored practical examples of how SWR and React Query simplify data management in various real-world scenarios, from paginated data and real-time chat applications to user authentication and e-commerce platforms. These libraries are versatile and adapt to your specific needs.
  • Real-time Updates: Both SWR and React Query excel in providing real-time reactivity and background updates. Your application will stay in sync with the server effortlessly, ensuring users see the most up-to-date data at all times.

The era of Axios and fetch was marked by challenges in managing data efficiently. SWR and React Query have stepped in to change the game. They make data fetching and management not just a task to complete but a joy to perform. As you embrace these modern tools in your React projects, you’ll find that your development process becomes more streamlined, your application more performant, and your users more satisfied.

As the future of data management in React, SWR and React Query offer a path to simplified, efficient, and real-time applications. It’s time to say farewell to the complexities of the past and welcome a brighter future where data management becomes an enjoyable and responsive experience.

Let’s continue building applications that set new standards for speed, responsiveness, and user satisfaction. Welcome to the era of modern data management with SWR and React Query!

Final Thoughts: Unlocking the Potential of SWR and React Query

In our journey through the world of data management in React, we've witnessed a transformation from traditional methods like Axios and fetch to the modern and powerful duo of SWR (Stale-While-Revalidate) and React Query. These libraries have reshaped the landscape of data management, opening up new possibilities and simplifying the developer experience.

As we conclude, it's essential to emphasize the key takeaways:

  • Efficiency and Developer-Friendly: SWR and React Query streamline data fetching, caching, state management, and error handling, all while keeping your application responsive. They provide the tools you need without adding unnecessary complexity to your codebase.

  • Real-World Use Cases: We’ve explored practical scenarios where SWR and React Query shine, from handling paginated data and real-time chat applications to user authentication and e-commerce platforms. These libraries adapt to a wide range of applications, simplifying data management in each case.

  • Real-Time Updates: The real-time capabilities of SWR and React Query ensure that your application remains in sync with the server, delivering the latest data to your users automatically.

This is crucial for building applications that provide immediate feedback and up-to-date information.

With SWR and React Query, you’re equipped to develop applications that set new standards for speed, responsiveness, and user satisfaction. These libraries simplify data management, allowing you to focus on building features and creating a delightful user experience.

As we bid adieu to the complexities of the past, we look forward to an era where data management becomes an enjoyable and responsive experience. The future of data management in React is here, and it's time to embrace it. Welcome to the world of SWR and React Query, where the potential is boundless, and the possibilities are endless.

Let's continue to innovate and build applications that shape the future of web development, one efficient data fetch at a time. Happy coding!

Top comments (3)

Collapse
 
chideracode profile image
Chidera Humphrey

Really love this article.

SWR and React Query promise a more intuitive way of handling data fetching. I haven't used the technology before, but with this article, I'm equipped with a practical knowledge.

In my latest project, I will use SWR and React Query.

But I have a question, can one use SWR for data fetching without React Query?

Collapse
 
nitin93 profile image
NK PESWANI

And yes, you can absolutely use SWR as a standalone library without React Query.

const fetcher = (url) => fetch(url).then((res) => res.json());

const { data, error } = useSWR('/api/data-endpoint', fetcher);

You can use like this... 👍👍

Collapse
 
nitin93 profile image
NK PESWANI

Thanks a lot for your appreciation 😊 I am really glad