
영상에서 고민하고 있는 redux store 코드 예시
위와 같은 것들을 모두 관리하는게 맞는 건가 ?

프론트에서 이값들이 저장되어있는 state들은 일종의 캐시
ex
만약 주문을 한다 -> 주문을 생성 -> 주문 데이터는 client 에서 관리해야하는 데이터가 아닌 서버에서 DB에 저장되어 있는 값, 서버에 관리하는 값서버에서 관리하는 값을 가져오기 위해서는 비동기 api 가 필요하다
즉, api 패칭을 해서 가져오든, 업데이트를 할때도 api가 필요하다다른사람과 공유되는 데이터, 내가 모르는 사이에 변경
(open order, 코인 주문 상태, 미체결 상태)
키포인트는 데이터의 Ownerchip이 있는 곳
Fetch, catch and update data in your React and React Native applications all without touching any "global state"
React Query is often described as the missing data-fetching library for React, but in more technical terms, it makes fetching, caching, sychronizing and updating server state in your React applications a breeze

react querysms zero-config로 즉시 사용가능, 원하면 언제든지 config도 커스텀 가능 ?
import {
QueryClient,
QueryClientProvider,
useQuery,
} from '@tanstack/react-query'
const queryClient = new QueryClient()
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
function Example() {
const { isPending, error, data } = useQuery({
queryKey: ['repoData'],
queryFn: () =>
fetch('https://api.github.com/repos/TanStack/query').then((res) =>
res.json(),
),
})
if (isPending) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message
return (
<div>
<h1>{data.name}</h1>
<p>{data.description}</p>
<strong>👀 {data.subscribers_count}</strong>{' '}
<strong>✨ {data.stargazers_count}</strong>{' '}
<strong>🍴 {data.forks_count}</strong>
</div>
)
}
queryClient를 provider로 넣어주고 시작을 한다
보통 get으로 받아올 대부분의 api에 사용할 아이, 데이터 Fetching 용이다

key, value 맵핑구조 생각하면 된다
React Query는 Query key에 따라 query caching을 관리한다
// a list fo todos
useQuery('todos', ...) // queryKey === ['todos']
// Something else, whatever
useQuery('somethingSpecial', ...) // queryKey === ['somethingSpecial']
// an individual todo
useQuery(['todo', 5], ...)
//queryKey === ['todo', 5]
// an individual todo in a "preview" format
useQuery(['todo', 5, { preview: true }], ...)
// queryKey === ['todo', 5, { preview: true }]
// alist of todos that are "done"
useQuery(['todos', { tupe: 'done' }], ...)
//queryKey === ['todos', { type: 'done' }]
DAta Fetching 할 때 promise 함수를 만든다



config 커스텀 ?




데이터 updating 시 사용하는 아이
즉, 데이터를 생성, 수정, 삭제하는 용 (create, update, delete)



optimistic update 란 ?
낙관적 업데이트는 한 마디로, 데이터를 변경하려 할 때, 응답을 기다리기 전 미리 UI를 업데이트 시키는 것이다.



위의 컨셉을 메모리 캐시에 적용 해보자



알아서 하는 것들이 있어서 좋지만 주의도 해야합니다


어떻게 Server State들을 전역상태처럼 관리할까 ?

정답은 Context api에 있다
QueryClient 내부적으로 Context를 사용하고 있다

Provider가 필요한 이유가 여기에 있다

보일러플레이트란?
컴퓨터 프로그래밍에서 보일러플레이트 또는 보일러플레이트 코드라고 부르는 것은 최소한의 변경으로 여러곳에서 재사용되며, 반복적으로 비슷한 형태를 띄는 코드를 말한다.보일러플레이트 코드 제거하기
매번 프로그래밍을 할 때마다 보일러플레이트 코드를 작성하는 것은 비효율적이고 귀찮다. 리팩토링을 하게 되면 보일러플레이트 코드도 같이 수정해야하는 경우도 많다