admin管理员组文章数量:1122846
I'm implementing a filtered list with pagination using React Query, and I'm experiencing a UI flicker when filters change. This happens because the page needs to reset to 1 when applying new filters.
// Query hook
export function usePatients({
initialData,
filters,
skip,
take,
}: {
initialData?: TFetchResponse<Patient>;
skip?: number;
take?: number;
filters?: PatientFilters;
}) {
return useQuery({
queryKey: ['patients', { skip }, { take }, { filters: JSON.stringify(filters) }],
queryFn: async () => {
const response = await patientFindManyAction({
skip,
take,
filters,
});
if (!response || !response.data) throw new Error('Something went wrong');
return response.data;
},
placeholderData: (previousData) => previousData ? previousData : initialData,
});
}
// Usage in component
function PatientList() {
const [page, setPage] = useState(1);
const [filters, setFilters] = useState<PatientFilters>();
const pageSize = 10;
// When filters change, we need to reset to page 1
useEffect(() => {
setPage(1);
}, [filters]);
const { data, isLoading } = usePatients({
skip: (page - 1) * pageSize,
take: pageSize,
filters
});
return (
// ... rendering logic
);
}
The Issue The sequence causing the flicker: 1. User applies new filter 2. Query starts fetching with new filters 3. Page state resets to 1 4. UI shows page 1 of OLD results while waiting 5. New filtered results arrive and replace the view This creates a jarring user experience where they briefly see the wrong data.
How can I prevent showing the old data with page 1 while the new filtered data is loading?
What's the correct pattern for handling this scenario with React Query?
本文标签: React Query Prevent flickering when filters changeStack Overflow
版权声明:本文标题:React Query: Prevent flickering when filters change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307896a1933469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论