
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-fast-fetch
Advanced tools
Fetch and cache remote data in React apps. Make your app load faster.
react-fast-fetch is a stale-while-revalidate implementation similiar to swr, with optimized support for IndexedDB.
npm install --save react-fast-fetch
Use default fetch and MemoryStore.
import { useFetch } from 'react-fast-fetch';
function App() {
const { data, loading, reload, error } = useFetch('https://unpkg.com/react/package.json');
return (
<div>
{loading ? (
<span>Loading...</span>
) : (
<>
{error ? (
<span>Failed to fetch data</span>
) : (
<span> Latest version of React is {data?.version}</span>
)}
<button onClick={reload}>Reload</button>
</>
)}
</div>
);
}
render(<App />);
react-fast-fetch supports 5 types of storage:
| Store | Persistence | Limit of size | I/O Speed |
|---|---|---|---|
| MemoryStore | ❌ | ~1GB | < 1ms |
| StorageStore (localStorage) | ✅ | 5MiB 😢 | < 1ms |
| StorageStore (sessionStorage) | ❌ | 5MiB 😢 | < 1ms |
| IndexedDBStore | ✅ | >10GB | 10~1000ms 😢 |
| CacheStore | ✅ | >10GB | 10~1000ms 😢 |
You can also use multiple different store in the same app, if you know what you really need!
import { MemoryStore } from 'react-fast-fetch';
export const store = new MemoryStore({
// maximum 2000 url to cache
limit: 2000,
});
import { StorageStore } from 'react-fast-fetch';
export const store = new StorageStore({
// or sessionStorage as you want
storage: localStorage,
// maximum 500 url to cache
limit: 500,
});
import { IndexedDBStore } from 'react-fast-fetch';
export const store = new IndexedDBStore({
// database name
dbName: 'my-store',
// maximum 5000 url to cache
limit: 5000,
});
const fetcher = (url) => fetch(url).then((res) => res.json());
import axios from 'axios';
const fetcher = (url) => axios.get(url).then((res) => res.data);
Here are two ways to configure react-fast-fetch.
// Use global config, recommended
<FetchProvider fetcher={fetcher} store={store}>
...
</FetchProvider>;
// Use local config, for flexibility
const { data } = useFetch('/url', { fetcher: customFetcher, store: customStore });
Both ways supports the following configuration:
Disable data fetching. This is useful when some parameters is required to fetch data.
const [page, setPage] = useState(1);
const { data } = useFetch(`/posts?page=${page}`, { disabled: page < 1 });
When url or params changes, preserve old data before new data loaded. Enable when you want smooth transition. However, old data may cause displaying wrong information. Use it carefully.
Typical usage example may want to enable this option:
Auto-reload data in N milliseconds. Use it to keep data up-to-date.
const { data } = useFetch(`/notifications/unread`, { interval: 5000 });
Callback when the intial load is done.
const [page, setPage] = useState(1);
const { data } = useFetch(`/posts?page=${page}`, {
onLoad: (url, data) => {
console.log(url, data);
},
});
Callback when data is reloaded.
const [page, setPage] = useState(1);
const { data } = useFetch(`/posts?page=${page}`, {
onReload: (url, data) => {
console.log(url, data);
},
});
Result data returned by fetcher.
const { data } = useFetch('/posts/1');
If here is NO cached data and fetcher is fetching data from remote, loading is set to true.
const { data, loading } = useFetch('/posts/1');
return (
<div>
{loading && <div>Loading...</div>}
{data && <div>{data.title}</div>}
</div>
);
If here is cached data and fetcher is fetching data from remote, reloading is set to true. In most cases, you don't need to notice user that it is reloading if the API is fast enough. If the API is indeed very slow, show some messages or progress bars that don't block user interaction.
const { data, loading } = useFetch('/posts/1');
return (
<div>
{loading && <div>Loading...</div>}
{reloading && <div>Refreshing...</div>}
{data && <div>{data.title}</div>}
</div>
);
A function to manually reload data from remote. Usually used in two cases:
Error throwed by fetcher. Usually mean user need to reload the data.
const { data, loading } = useFetch('/posts/1');
return (
<div>
{loading && <div>Loading...</div>}
{error && (
<span>
Failed to fetch data <button onClick={reload}>Reload</button>
</span>
)}
{data && <div>{data.title}</div>}
</div>
);
FAQs
Fetch and cache remote data in React apps
We found that react-fast-fetch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.