Middle
Do you have experience with React Query or SWR libraries for caching state on the request side?
sobes.tech AI
Answer from AI
Yes, I have experience working with both libraries — React Query and SWR. Both allow efficient caching of data received from the server and managing loading states, errors, and updates.
React Query offers more extensive features, including automatic data updates, background refreshes, mutation management, and complex caching strategies. SWR is simpler and lighter, ideal for projects that require minimal setup and quick start.
An example of using React Query to fetch a list of users:
import { useQuery } from 'react-query';
function Users() {
const { data, error, isLoading } = useQuery('users', () =>
fetch('/api/users').then(res => res.json())
);
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Loading error</div>;
return (
<ul>
{data.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
Thus, both libraries significantly simplify working with asynchronous requests and client-side caching.