September 27, 2024 • ☕️ 5 min read
staleTime vs gcTime
staleTime: number | ((query: Query) => number)
0Infinity로 설정 시 데이터는 절대 stale하다고 취급되지 않음gcTime: number | Infinity
5 * 60 * 1000(5분)gcTime이 지난 후에 가비지 컬렉션 됨Infitinity로 설정 시 가비지 컬렉션을 하지 않음refetchInterval vs refetchIntervalInBackground
refetchInterval: number | false | ((query: Query) => number | false | undefined)
refetchIntervalInBackground: boolean
true로 설정 시 refetchInterval로 반복적으로 refetch하도록 설정된 쿼리들은 탭/백그라운드에서도 계속 refetch를 수행함queryClient.setQueryData vs queryClient.fetchQuery
queryClient.setQueryData
gcTime인 5분 동안 사용되지 않으면, 해당 쿼리는 가비지 컬렉트된다.queryClient.setQueriesData를 사용할 수 있다.queryClient.fetchQuery
staleTime보다 오래 됐다면, 캐시의 데이터를 반환한다. 아니면 가장 최근 데이터를 fetch한다.setQueryData는 동기적이고 이미 동기적으로 사용 가능한 데이터가 있음을 가정한다. 만약 데이터를 비동기로 fetch하고 싶다면 쿼리 키를 refetch하거나 fetchQuery를 이용하여 비동기 fetch를 수행하는 것을 권장한다.fetchQuery는 비동기적이고 데이터가 조회되는 동안 동일한 쿼리에 대해 중복 요청이 생성되지 않도록 보장하는 반면, useQuery 인스턴스가 동일한 쿼리를 렌더링할 때 setQueryData는 동기적으로 작동한다.Ref https://tanstack.com/query/latest/docs/reference/QueryClient/
useMutation으로 업데이트한 데이터 바로 보여주기queryclient의 setQueryData 사용하기
const queryClient = useQueryClient()
const mutation = useMutation({
mutationFn: editTodo,
onSuccess: (data) => {
queryClient.setQueryData(['todo', { id: 5 }], data)
},
})
mutation.mutate({
id: 5,
name: 'Do the laundry',
})
const { status, data, error } = useQuery({
queryKey: ['todo', { id: 5 }],
queryFn: fetchTodoById,
})queryClient의 invalidateQueries 호출
클라이언트 데이터 기반의 낙관적 업데이트(Optimistic Update)
Ref
text-underline-position: under;text-underline-offset px 주기 (일부 브라우저 미지원)border-bottom과 padding-bottom으로 조정하기Ref https://hianna.tistory.com/680
infinite scroll 말고!
useQuery와 keepPreviousData 옵션을 사용할 수 있다.
🧑🏫 v5에서는
placeholderData에keepPreviousData를 import해서 사용한다.
Ref
기존에 Shadow DOM을 사용하려면 JavaScript가 필요했다.
const host = document.getElementById('my-elem')
const shadowRoot = host.attachShadow({ mode: 'open' })
shadowRoot.innerHTML = '<h1>안녕하세요?</h1>'그런데 Declarative Shadow DOM(선언적 Shadow DOM) 기능을 통해 JavaScript 없이도 Shadow DOM을 만들 수 있게 되었다! 😲
<div id="my-elem">
<template shadowrootmode="open">
<style>h1 { color: red; }</style>
<h1>나는 여기서만 빨간색이에요</h1>
</template>
</div>vscode에서 git diff를 확인하기 좋은 익스텐션!
Ref https://marketplace.visualstudio.com/items?itemName=pomber.git-file-history
지난주에 TypeScript 5.6 릴리즈를 포스팅했었는데, 바로 5.7 플랜이라니 😵 바쁘다 바빠.
Ref https://github.com/microsoft/TypeScript/issues/59905
밴드 녹음🎤 에 짝꿍 쇼핑🛍️에 가족 일정까지
바쁘디 바쁘지만 시원해서 좋았던 주말이었다
9월도 이제 끝나는구만