React Query not updating my list on first render
My list is not updating when i come back after updating my list , but my api is returning updated data
export default function App() {
const [id] = useQueryState("category-id", parseAsInteger);
const queryKey = useMemo(() => ["list", id], [id]);
const { data: list,isLoading } = useQuery({
queryKey,
queryFn: () => getList({ categoryId: id || null }),
enabled: !!id,
refetchOnMount:'always',
staleTime:0
});
const isList1Empty = useMemo(() => {
if (id !== 1 && !list) return false;
return list.length === 0;
}, [id,list]);
const isList2Empty = useMemo(() => {
if (id !== 2 && !list) return false;
return list.length === 0;
}, [id,list]);
useEffect(() => {
if (!list) return;
if (
(id === 1 && isList1Empty) ||
(id=== 2 && isList2Empty)
) {
// navigate to update list page
}
}, [id,list]);
if (isLoading) {
return (
<h1>loading</h1>
);
}
return <div>{id === 1 ? <List1 list={list} /> : <List2 list={list} />}</div>;
}
Steps :
- Page loaded
- get list api called
- checks if list is empty
- if empty redirect to update form page
- updated my list
- comes back to this page
- getList returns updated list but its not updated on my ui listing on first mount
refetchOnMount: 'always'?