[ad_1]
Fetching knowledge in React typically means utilizing stale state and sophisticated caching logic.
React Question simplifies knowledge fetching with highly effective options like computerized caching, deduplication, and background updates.
Let’s discover some key options of React Question:
Declarative Knowledge Fetching
Fetch knowledge with the useQuery
hook:
import { useQuery } from 'react-query';
operate MyComponent() {
const { knowledge, error, isLoading } = useQuery('posts', fetchPosts);
// use knowledge
}
useQuery
handles declaring cache keys, performing fetches, and extra.
background Refetching
React Question robotically refetches “inactive” queries within the background:
// frontend
useQuery('person', fetchUser);
// background
// periodically refetches person
Stale knowledge is up to date with out blocking the UI.
Request Deduplication
Duplicate requests are deduped to stop wasteful refetches:
operate PageOne() {
useQuery('posts', fetchPosts);
}
operate PageTwo() {
useQuery('posts', fetchPosts); // not duplicated
}
React Question shares cache outcomes throughout elements.
Optimistic Updates
Mutations can replace the cache optimistically earlier than fetching:
const mutation = useMutation(addPost, {
onMutate: newPost => {
// replace cache instantly
},
onError: (err, newPost, context) => {
// rollback optimistic replace
},
});
This makes the UI really feel quick and responsive.
Abstract
- Simplifies knowledge fetching with useQuery
- Refetches stale knowledge in background
- Deduplicates requests robotically
- Optimistic updates make UI really feel snappy
React Question takes the ache out of async knowledge administration!
[ad_2]