Skip to content

Commit

Permalink
fix: SSE 수신 시 실행되는 setQueryData가 제대로 작동하지 않는 문제 해결
Browse files Browse the repository at this point in the history
- 기존 로직의 경우 이전 값인 old의 값을 그대로 변경해 참조가 유지되었음
- 참조가 그대로 유지되면 tanstack-query는 값이 변경되지 않았다고 판단되어 갱신이 일어나지 않을 수 있음
- 따라서, 새로운 변수 newThread를 만들어 이를 반환하는 것으로 참조를 변경
  • Loading branch information
wzrabbit committed Mar 23, 2024
1 parent 347e43b commit 812ae4b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions frontend/src/hooks/queries/useSSE.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback, useEffect } from 'react';
import { type InfiniteData, useQueryClient } from '@tanstack/react-query';
import { useQueryClient } from '@tanstack/react-query';
import type { InfiniteData } from '@tanstack/react-query';
import { baseUrl } from '~/apis/http';
import { EventSourcePolyfill } from 'event-source-polyfill';
import { useToken } from '~/hooks/useToken';
import { useTeamPlace } from '~/hooks/useTeamPlace';
import { type ThreadsResponse } from '~/apis/feed';
import type { ThreadsResponse } from '~/apis/feed';

export const useSSE = () => {
const queryClient = useQueryClient();
Expand Down Expand Up @@ -34,11 +35,20 @@ export const useSSE = () => {

queryClient.setQueryData<InfiniteData<ThreadsResponse>>(
['threadData', teamPlaceId],
(old) => {
if (old) {
old.pages[0].threads = [newThread, ...old.pages[0].threads];
(oldData) => {
if (oldData) {
const newFirstPageThreads = {
threads: [newThread, ...oldData.pages[0].threads],
};
const newData = {
pageParams: oldData.pageParams,
pages:
oldData.pages.length === 1
? [newFirstPageThreads]
: [newFirstPageThreads, ...oldData.pages.slice(1)],
};

return old;
return newData;
}
},
);
Expand Down

0 comments on commit 812ae4b

Please sign in to comment.