@umeshp1212/react-fetch-cache
v1.0.0
Published
`@react-codex/react-fetch-cache` A custom React hook that simplifies data fetching with caching, a loading state, and error handling. It is built on top of @react-codex/fetch-cache-core.
Readme
@react-codex/react-fetch-cache
A custom React hook that simplifies data fetching with caching, a loading state, and error handling. It is built on top of @react-codex/fetch-cache-core.
Installation
npm install @react-codex/react-fetch-cache
or
pnpm install @react-codex/react-fetch-cache
Usage The hook is designed to be used in any functional React component. It provides a simple API for accessing the fetched data, loading state, and any errors.
import React from 'react';
import { useFetchCache } from '@react-codex/react-fetch-cache';
// Define the shape of the data you expect to receive from the API
type User = {
id: number;
name: string;
email: string;
};
const UserList = () => {
// Use the useFetchCache hook to fetch data
const { data, loading, error } = useFetchCache<User[]>('[https://jsonplaceholder.typicode.com/users](https://jsonplaceholder.typicode.com/users)');
if (loading) {
return <p>Loading users...</p>;
}
if (error) {
return <p>Error: {error.message}</p>;
}
return (
<ul>
{data && data.map(user => (
<li key={user.id}>
<strong>{user.name}</strong> - {user.email}
</li>
))}
</ul>
);
};
export default UserList;