fix: optimized rerenders caused by useList hooks

This commit is contained in:
2025-08-26 10:21:11 +04:00
parent 226e52a1c6
commit e0f86f2018
4 changed files with 67 additions and 54 deletions

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { BoardSchema } from "@/lib/client"; import { BoardSchema } from "@/lib/client";
import { getBoardsOptions } from "@/lib/client/@tanstack/react-query.gen"; import { getBoardsOptions } from "@/lib/client/@tanstack/react-query.gen";
@ -8,24 +8,27 @@ type Props = {
}; };
const useBoardsList = ({ projectId }: Props) => { const useBoardsList = ({ projectId }: Props) => {
const queryClient = useQueryClient();
const [boards, setBoards] = useState<BoardSchema[]>([]); const [boards, setBoards] = useState<BoardSchema[]>([]);
const { data, refetch, isLoading } = useQuery({ const fetchBoards = () => {
...getBoardsOptions({ path: { projectId: projectId! } }), if (!projectId) return;
enabled: projectId !== undefined, queryClient
}); .fetchQuery({
...getBoardsOptions({ path: { projectId } }),
})
.then(data => {
setBoards(data.boards);
})
.catch(err => console.error(err));
};
useEffect(() => { useEffect(() => {
if (projectId === undefined) { fetchBoards();
setBoards([]); }, [projectId]);
return;
}
if (data?.boards) {
setBoards(data.boards);
}
}, [data?.boards, projectId]);
return { boards, setBoards, refetch, isLoading }; return { boards, setBoards, refetch: fetchBoards };
}; };
export default useBoardsList; export default useBoardsList;

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { DealSchema } from "@/lib/client"; import { DealSchema } from "@/lib/client";
import { getDealsOptions } from "@/lib/client/@tanstack/react-query.gen"; import { getDealsOptions } from "@/lib/client/@tanstack/react-query.gen";
@ -10,22 +10,27 @@ type Props = {
const useDealsList = ({ boardId }: Props) => { const useDealsList = ({ boardId }: Props) => {
const [deals, setDeals] = useState<DealSchema[]>([]); const [deals, setDeals] = useState<DealSchema[]>([]);
const { data, refetch, isLoading } = useQuery({ const queryClient = useQueryClient();
...getDealsOptions({ path: { boardId: boardId! } }),
enabled: boardId !== undefined, const fetchDeals = () => {
if (!boardId) return;
queryClient
.fetchQuery({
...getDealsOptions({ path: { boardId } }),
})
.then(data => {
setDeals(data.deals);
})
.catch(err => {
console.error(err);
}); });
};
useEffect(() => { useEffect(() => {
if (boardId === undefined) { fetchDeals();
setDeals([]); }, [boardId]);
return;
}
if (data?.deals) {
setDeals(data.deals);
}
}, [data?.deals, boardId]);
return { deals, setDeals, refetch, isLoading }; return { deals, setDeals, refetch: fetchDeals };
}; };
export default useDealsList; export default useDealsList;

View File

@ -1,27 +1,29 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { ProjectSchema } from "@/lib/client"; import { ProjectSchema } from "@/lib/client";
import { getProjectsOptions } from "@/lib/client/@tanstack/react-query.gen"; import { getProjectsOptions } from "@/lib/client/@tanstack/react-query.gen";
const useProjectsList = () => { const useProjectsList = () => {
const { refetch, data, isLoading } = useQuery({ const queryClient = useQueryClient();
...getProjectsOptions(),
});
const [projects, setProjects] = useState<ProjectSchema[]>([]); const [projects, setProjects] = useState<ProjectSchema[]>([]);
const refetchProjects = async () => { const fetchProjects = () => {
const res = await refetch(); queryClient
setProjects(res.data?.projects ?? []); .fetchQuery({
...getProjectsOptions(),
})
.then(data => {
setProjects(data.projects);
})
.catch(err => console.error(err));
}; };
useEffect(() => { useEffect(() => {
if (data?.projects) { fetchProjects();
setProjects(data.projects); }, []);
}
}, [data?.projects]);
return { projects, setProjects, refetch: refetchProjects, isLoading }; return { projects, setProjects, refetch: fetchProjects };
}; };
export default useProjectsList; export default useProjectsList;

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { StatusSchema } from "@/lib/client"; import { StatusSchema } from "@/lib/client";
import { getStatusesOptions } from "@/lib/client/@tanstack/react-query.gen"; import { getStatusesOptions } from "@/lib/client/@tanstack/react-query.gen";
@ -8,24 +8,27 @@ type Props = {
}; };
const useStatusesList = ({ boardId }: Props) => { const useStatusesList = ({ boardId }: Props) => {
const queryClient = useQueryClient();
const [statuses, setStatuses] = useState<StatusSchema[]>([]); const [statuses, setStatuses] = useState<StatusSchema[]>([]);
const { data, refetch, isLoading } = useQuery({ const fetchStatuses = () => {
...getStatusesOptions({ path: { boardId: boardId! } }), if (!boardId) return;
enabled: boardId !== undefined, queryClient
}); .fetchQuery({
...getStatusesOptions({ path: { boardId } }),
})
.then(data => {
setStatuses(data.statuses);
})
.catch(err => console.error(err));
};
useEffect(() => { useEffect(() => {
if (boardId === undefined) { fetchStatuses();
setStatuses([]); }, [boardId]);
return;
}
if (data?.statuses) {
setStatuses(data.statuses);
}
}, [data?.statuses, boardId]);
return { statuses, setStatuses, refetch, isLoading }; return { statuses, setStatuses, refetch: fetchStatuses };
}; };
export default useStatusesList; export default useStatusesList;