- Next.js 15
- React 19
- SWR
- TypeScript
- Tailwind CSS
- file-saver
- unsplash api
- param1: Endpoint URL (string or function that returns a string).
- param2: Fetcher function (usually an API call, for example using Axios).
- param3: Options object (optional, for controlling behavior like revalidation, retries, etc.).
- Description: Specifies how often the data should be re-fetched in milliseconds. This is useful for polling or live-updating data.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { refreshInterval: 10000 });
- Description: When set to
true
, SWR will re-fetch the data when the window or tab regains focus. This is useful for showing fresh data when the user returns to the app. - Example:
const { data, error } = useSWR('/api/data', fetcher, { revalidateOnFocus: true });
- Description: When
true
, SWR will re-fetch the data when the internet connection is restored after being lost. - Example:
const { data, error } = useSWR('/api/data', fetcher, { revalidateOnReconnect: true });
- Description: Refetches the data when the component mounts. This is enabled by default, so it's typically not necessary to set this manually unless you want to override behavior.
- Example: This is the default behavior for SWR. You do not typically need to explicitly set it.
- Description: Specifies how often requests to the same key will be deduplicated in milliseconds. SWR deduplicates requests to the same key that occur within this time window to prevent unnecessary network requests.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { dedupingInterval: 2000 });
- Description: A boolean value specifying whether SWR should automatically retry the fetch when an error occurs.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { shouldRetryOnError: true });
- Description: A callback function that is invoked when the data fetch is successful.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { onSuccess: (data) => console.log(data) });
- Description: A callback function that is invoked when the data fetch fails.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { onError: (error) => console.log(error) });
- Description: A function that can be used to control retry behavior when an error occurs.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { onErrorRetry: (error, key, config, revalidate, { retryCount }) => { if (retryCount >= 3) return; setTimeout(() => revalidate({ retryCount }), 5000); // Retry every 5 seconds } });
- Description: This allows you to manually update the cache for a given key. It's commonly used for optimistic UI updates.
- Example:
import { mutate } from 'swr'; mutate('/api/data', updatedData);
- Description: Specifies a custom cache for SWR. By default, SWR uses an in-memory cache, but you can provide your own cache implementation.
- Example:
const customCache = new Map(); const { data, error } = useSWR('/api/data', fetcher, { cache: customCache });
- Description: When set to
true
, SWR will throw a promise to be handled by React's Suspense, which lets you wait for data before rendering the component. - Example:
const { data, error } = useSWR('/api/data', fetcher, { suspense: true });
- Description: Specifies the initial data to use before the data is fetched. This can be useful when you already have some data to show when the component first renders.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { initialData: [] });
- Description: Specifies the time in milliseconds for how long the data should be cached. By default, data is cached for 5 minutes.
- Example:
const { data, error } = useSWR('/api/data', fetcher, { cacheTime: 10000 }); // Cache data for 10 seconds
const { data, error } = useSWR('/api/data', fetcher);
const params = { per_page: 10, query: 'nature' }; // Example query parameters
useSWR(['/photos', params], fetcher); // Pass both URL and params
useSWR(
() => {
if (query) {
return `/search/photos?query=${query}&per_page=${count}`;
} else {
return `/photos?per_page=${count}`;
}
},
fetcher
);
SWR documentation for more details.
- Jonel Hatwell - [LinkedIn](https://www.linkedin.com/in/jonel-hatwell/)
- Unsplash API - [Unsplash](https://unsplash.com/)