SWR
Web framework / library · Free
React data fetching hooks with caching and revalidation: they display current server data and request states in the interface.
Also known as: vercel/swr; swr; stale-while-revalidate

What you can use for free
Code and local use under MIT.
Paid features and costs
Computing resources, self-hosting, and external services are paid for separately if used.
Access and limitations
Code and local use under MIT. Computing resources, self-hosting, and external services are paid for separately if used.
Use cases
- Add cached data fetching to a React screen
- Connect the hook to the component's loading, error, and revalidation states
Example workflow
A possible workflow; this is not a report of hands-on testing.
Add cached data fetching to a React screen.
Before you start
- Web application source code and the intended workflow.
- The project's framework and runtime version.
Steps
- Define a data key and fetcher for the required API.
- Connect the hook to the component's loading, error, and revalidation states.
- Add real data and error handling; check the production build.
Check your result
- The workflow works after a page refresh and with invalid data.
- The bundle, server rendering, and supported browsers suit the project.
Choosing SWR
Add cached data fetching to a React screen
Choose this when
- Add cached data fetching to a React screen
- Connect the hook to the component's loading, error, and revalidation states
Consider alternatives when
- When choosing for this topic, compare SWR with TanStack Query. A candidate to compare under the topic: State and client data, Web interface tools. This is an editorial connection by topic, not a confirmed alternative or a guarantee that the products work together.
- When choosing for this topic, compare SWR with Immer. A candidate to compare under the topic: State and client data. This is an editorial connection by topic, not a confirmed alternative or a guarantee that the products work together.
Requirements
- Define stable keys, a fetcher, and separate loading/error states.
Things to consider
- Costs and limits of free use: Computing resources, self-hosting, and external services are paid for separately if used.
- Terms of use: MIT for the code; dependencies have separate licenses.
- A cache does not replace server permissions or important data freshness checks. The fetcher may use a chosen network/API and its terms.
Frequently asked questions
What can you use for free in SWR?
Code and local use under MIT.
What additional costs might arise with SWR?
Computing resources, self-hosting, and external services are paid for separately if used.
What terms of use matter for SWR?
MIT for the code; dependencies have separate licenses.
How do you get started with SWR?
Define a data key and fetcher for the required API. Connect the hook to the component's loading, error, and revalidation states. Add real data and error handling; check the production build.
What does SWR do with stale data, and what is a fetcher?
SWR first returns cached data, then revalidates the request and updates the result. useSWR accepts a key and asynchronous fetcher, returning data, error, and isLoading. Deduplication, focus/network recovery revalidation, polling, optimistic mutation, and SSR/SSG are supported; the fetcher chooses the transport.
Related tasks and approaches
- TanStack Query
A candidate to compare under the topic: State and client data, Web interface tools. This is an editorial connection by topic, not a confirmed alternative or a guarantee that the products work together.
- Immer
A candidate to compare under the topic: State and client data. This is an editorial connection by topic, not a confirmed alternative or a guarantee that the products work together.
- Jotai
A candidate to compare under the topic: State and client data. This is an editorial connection by topic, not a confirmed alternative or a guarantee that the products work together.
License
MIT for the code; dependencies have separate licenses.
MIT — permissive license
You can use, modify, distribute, and sell the code, including in a closed-source commercial product. When sharing copies or substantial portions of the code, retain the license text and copyright notice. You do not need to open-source your entire project; the author provides no warranties.
License termsTechnologies and topics
- SWR
- web
- Development