How we implemented SWR in a project and why we loved it
SWR, a React Hooks library for remote data fetching, provides a simple yet powerful solution for caching, revalidating, and retrieving data in React applications. Learn how SWR eliminates the complexities of managing state slices, speeds up development, and enhances UI performance. Explore examples of SWR implementation and discover its integrations.

Kamil Polok
Jun 09, 2021・6 min read
Why SWR?
Recently, we had the opportunity at SDH to develop a project using React with support from Redux/Toolkit. Thing is, though data was kept and fetched in the store, there was no caching mechanism implemented in the app. And holding dozens of responses in the state is not my idea of a cool idea. Whatever the case, it soon got a little too easy to get lost in all those state slices, despite the use of Toolkit.
What’s more, it had zero influence on the backend at that point. With those iceberg-sized chunks of data drifting about, some requests took an eternity to resolve. And since the UI’s application consisted of long item lists with few CRUD operations, it soon became clear that some form of cache would come in pretty handy just about then. So the choice fell to SWR - a library made by the developers of Vercel (also of Next.js fame).
What is SWR?
Ok, do you like reading definitions? I hope not, because this is going to be one of the shortest you've ever read. ‘SWR is a React Hooks library for remote data fetching’. That's it. Simple? Yes, but I will now show you that SWR is also very powerful. And in case you were wondering, SWR stands for stale-while-revalidate. Which already explains how it works. SWR returns data from cache (stale), sends a fetch request to get updated data (revalidate), then returns new data. Like I said, simple.
Why we loved SWR
It’s easy to recognise how using SWR eliminates problems described at the beginning of this article. Thanks to SWR, we:
- Made the UI work perfectly (after initial data requests) thanks to caching data
- Removed nearly all slices holding API data and accompanying logic
- Simplified the flow of revalidating data after making CRUD operations on data collections
- Sped up the development of new API-dependent features
How did we actually implement this library for such magical results?
How SWR works
After a short while of working with SWR, it proved itself to be rather simple and quite user-friendly. Fancy making an API request using SWR? Have this:

The only catch is the fetcher. It’s just your custom function for fetching data. The application we worked on already had axios set up and running with its entire config (interceptors, headers, base URL, etc.), so we just wrapped our axios instance like this to get our own custom fetcher:

Therefore, the entire hook could look as simple as this (let's skip error handling in this example):

And... that's it! We’ve created a hook for fetching API data using all the SWR goodies. Whenever this hook is fired, it will return previously fetched data first (if present), and only then revalidate data and return new collection afterwards.
SWR out of the box comes with a handful of pre-configured parameters (all are listed here), which make working with it a piece of cake. A favorite option worth mentioning is dedupingInterval.
Typical use-case scenario: fetch data in one component, keep developing this component further, watch children grow out of control, only for a 4th level child to need the data. How you get this data into the child component without much effort whilst avoiding repeated requests is always a challenge. But fear not: simply reuse the SWR hook... The deduping interval will not allow the request to be made again within a specified amount of time (2 seconds by default). Fair enough.
"But if I call the endpoint, and it returns the same data as before, my component will still re-render". It will not: SWR implements deep comparison so re-render will only be triggered when data has actually changed. And the comparison function can, of course, be customized.
Data mutation
But you might argue that GETting data is always easy - what about the mutating part? Surely this is more complicated. Well, not really, mutations don't have to be a nightmare, and I was surprised how easy it is to mutate data using SWR (previously having had some fun with Apollo). Here's a real-life example:

In this example mutate is a function imported from SWR and /users is the key we gave it in useUsers hook (It doesn't have to be the endpoint URL - you can pass the key and URL separately).
Should you wish to cache and update a single entity (object) fetched from a bigger collection, simply mutate it by its key like so:

Looks too simple? It is simple. Try it for yourself and you just may save considerable time writing extensive Redux logic and debugging your API calls.
SWR integrations
As you can see, implementing SWR is simple; with a few nice touches, it doesn’t take much to advance the above examples to higher, more abstract levels. Obviously, SWR is not limited to this and there is a great number of options and integrations available:
graphQL integration
error handling with retry options
auto revalidation
pagination support (also for infinite loading)
data prefetching
and many more (documentation)
We found working with SWR supremely cool and will definitely consider using it for future projects. If you’d like to learn more about this awesome library, visit their official website with documentation, configs and examples here.
Happy coding!

Digital Transformation Strategy for Siemens Finance
Cloud-based platform for Siemens Financial Services in Poland

You may also like...

How to generate PDF from HTML in React / Node.js app
Generating PDFs from HTML can be a challenge, but with the right methods, you can achieve the desired results. Explore different solutions, from using CSS print rules to libraries like html2canvas, jsPDF, and Puppeteer. Understand the pros and cons of each approach to find the best solution for your project. Contact us at hello@start-up.house for more information and assistance. Good luck in your PDF generation endeavors!

Eugene Zolotarenko
May 20, 2021・6 min read

4 reasons to use Chakra UI in your next project
Chakra UI is a simple, modular, and accessible component library for building React apps with speed. With its easy customization, dark mode support, responsive design capabilities, and focus on accessibility, Chakra UI stands out among other UI libraries, making it an excellent choice for React developers.

Mateusz Wójcik
Jan 19, 2021・4 min read

Smart Mirrors in Retail: Enhancing Shopping Experiences with Technology
Smart mirrors are transforming retail stores by offering interactive shopping experiences, virtual try-ons, and personalized recommendations. With features like augmented reality and real-time inventory integration, these technologies enhance customer engagement and streamline the in-store experience.

Alexander Stasiak
May 14, 2024・9 min read

AI Demand Planner: How It Works and How to Implement It
Market volatility, supply chain disruptions, and rising consumer expectations mean that traditional forecasting methods based on historical averages are no longer sufficient. AI Demand Planner is an advanced technology solution that leverages artificial intelligence and machine learning to accurately predict future demand for products and services. It transforms static data into dynamic business scenarios, enabling companies to optimize inventory and improve operational efficiency.

Alexander Stasiak
Nov 04, 2026・11 min read

S&OP Implementation – Technical Architecture
Effective Sales and Operations Planning (S&OP) is no longer just a process—it’s a technology challenge. In a modern enterprise, the technology architecture of an S&OP implementation is the foundation that determines whether the organization can respond to market volatility in real time or gets bogged down in outdated spreadsheets.

Alexander Stasiak
Nov 02, 2026・9 min read

S&OP Software – Purpose-Built AI Solutions
Modern supply chain management requires more than just a reactive approach to market shifts. S&OP software—purpose-built and AI-powered—is becoming the foundation for companies looking to synchronize sales planning with operational capabilities in a dynamic, fact-based way rather than relying on intuition. In an era of rising volatility, traditional spreadsheets are no longer enough, creating error risks that directly erode profitability.

Alexander Stasiak
Nov 03, 2026・9 min read
Recently added

AI Demand Planner: Demand Forecasting and S&OP in Food Manufacturing
Managing the modern supply chain in the food manufacturing and FMCG sector requires more than just accurate reporting of the past. In the face of increasing market volatility, traditional spreadsheet-based demand planning methods built on moving averages are no longer sufficient.

Alexander Stasiak
Nov 13, 2026・9 min read

AI-Powered S&OP – Automating Sales and Operations Planning
In today’s rapidly changing business landscape, traditional Sales & Operations Planning (S&OP) methods often can’t keep up with market volatility. Manufacturing and trading companies in Poland face disrupted supply chains, sharp demand swings, and information silos that hinder optimal decision-making. AI‑powered S&OP—automating Sales & Operations Planning—is a strategic answer to these challenges, shifting organizations from reactive management to proactive forecasting.

Alexander Stasiak
Nov 12, 2026・9 min read

Demand Forecasting in the Food Industry: How to Leverage AI and S&OP to Reduce Losses?
Managing the supply chain in the food industry is like walking a tightrope. On one side you’re dealing with short shelf lives; on the other, rapid shifts in consumer preferences and seasonality. Forecasting errors are costly—according to market data, the food industry loses billions of euros every year to food waste and lost sales opportunities.

Alexander Stasiak
Nov 11, 2026・11 min read

Demand Forecasting in Manufacturing: How to Integrate AI
In modern manufacturing, intuition-driven planning and simple moving averages are no longer enough. Rising market volatility, supply chain disruptions, and pressure to optimize costs are pushing operations leaders to seek more precise tools. Demand forecasting in manufacturing—how to integrate AI with the existing IT architecture—has become one of the most important questions for the C‑suite today.

Alexander Stasiak
Nov 10, 2026・10 min read

S&OP in Manufacturing: How to Synchronize Demand
Effective management of a modern manufacturing operation requires moving beyond siloed planning. S&OP in manufacturing—how to synchronize demand, production, and inventory—is the key question facing organizations striving to optimize costs and operations. In an era of rapid market shifts, traditional methods based on intuition and static spreadsheets no longer suffice, giving way to advanced data analytics.

Alexander Stasiak
Nov 09, 2026・9 min read

Demand Forecasting in Manufacturing – Leveraging AI
Effective demand forecasting in manufacturing—AI is becoming the foundation of the modern supply chain. In an era of global market volatility, traditional statistical methods based on moving averages often fall short, leading to costly planning errors. Implementing advanced machine learning algorithms enables organizations to transform reactive planning into a proactive business strategy.

Alexander Stasiak
Nov 08, 2026・8 min read
Ready to centralize your know-how with AI?
Start a new chapter in knowledge management—where the AI Assistant becomes the central pillar of your digital support experience.
Work with a team trusted by top-tier companies.
