Optimize data querying for React and React Query to only occur when the component is in the viewport
P粉714890053
2023-08-28 17:26:18
<p>I have a page that renders a list of post components using React, and for each post component I get the comments related to that post and display them inside using the React Query library. The problem is that when the user loads the post page, React Query loads all comments for all posts at once, which causes my backend to slow down. </p>
<p>I want to implement a mechanism that only loads comments when the user scrolls to a specific post, instead of loading everything at once when the page renders. How to achieve this using React and React Query? </p>
<p>This is my current sample code to get and display comments: </p>
<p>My Post Component</p>
<p>
<pre class="brush:js;toolbar:false;">import React from 'react';
import { useQuery } from 'react-query';
const PostComponent = ({ post }) => {
const { data, isLoading, error } = useQuery(
['comments', post.id],
() => fetchComments(post.id)
);
return (
<div>
{/* Render your post component */}
{isLoading && <div>Loading comments...</div>}
{error && <div>Error getting comments</div>}
{data && (
<div><p>{post.title}</p> </div>
)}
</div>
);
};</pre>
</p>
<p>My Post Page Component
<pre class="brush:js;toolbar:false;">import React from 'react';
import { useQuery } from 'react-query';
const PostsPage = () => {
const { data, isLoading, error } = useQuery('posts', fetchPosts);
return (
<div>
{isLoading && <div>Loading posts...</div>}
{error && <div>Error getting posts</div>}
{data &&
data.map((post) => (
<PostComponent key={post.id} post={post} />
))}
</div>
);
};</pre>
</p>
To achieve this goal, you need two components.
Intersection Observer API
in your React app, you should use the useInViewhook from the
react-intersection-observerlibrary .