// useGetAnyInformation.js
const useGetAnyInformation = ({ boardId } ) => {
const get = () => {
const response = api.get(`...?id=${boardId}`);
return response?.data;
}
return useQuery({
queryKey: ['anyInformation', boardId];
queryFn: get
})l
}
// component.vue
<template>
<span> this is information component </span>
<button @click="() => boardId = boardId += 1;">
</template>
<script setup>
import { ref, watch } from 'vue';
const boardId = ref(0);
const { data } = useGetAnyInformation({ boardId: boardId.value });
</script>// useGetAnyInformation.js
const useGetAnyInformation = (getParams) => {
const get = () => {
const { boardId } = getParams();
const response = api.get(`...?id=${boardId}`);
return response?.data;
}
return useQuery({
queryKey: ['anyInformation', boardId];
queryFn: get
})l
}
// component.vue
<template>
<span> this is information component </span>
<button @click="() => boardId = boardId += 1;">
</template>
<script setup>
import { ref, watch } from 'vue';
const boardId = ref(0);
// 매번 함수로 넘겨주면서 리패칭을 하게 되면 boardId를 최신화
const { data, refetch } = useGetAnyInformation(() => { boardId: boardId.value });
// boardId를 감지하기 위해 추가된 부분
watch(boardId, () => {
refetch();
});
</script>// useGetAnyInformation.js
const useGetAnyInformation = (boardIdRef) => {
const get = () => {
const response = api.get(`...?id=${boardIdRef.value}`);
return response?.data;
}
return useQuery({
queryKey: ['anyInformation', boardIdRef];
queryFn: get
})l
}
// component.vue
<template>
<span> this is information component </span>
<button @click="() => boardId = boardId += 1;">
</template>
<script setup>
import { ref, watch } from 'vue';
const boardIdRef = ref(0);
// 매번 함수로 넘겨주면서 리패칭을 하게 되면 boardId를 최신화
const { data, refetch } = useGetAnyInformation(boardIdRef);
</script>...
const queryClient = new QueryClient();
const data = await queryClient,fetchQuery({
queryKey: ['anyInformation', boardId];
queryFn: async () => {
const response = api.get(`...?id=${boardIdRef.value}`);
return response?.data;
}
});
console.log(data) // data.value로 접근을 하는 것이 아니라, 직접 data에 접근하면 된다.// react
const [boardId, setBoardId] = useState('');
useQuery({
queryKey: ['board', boardId], // 일반 문자열로 인식
queryFn: ...
});
// vue
const boardId = ref('');
useQuery({
queryKey: ['board', boardId], // `ref` 반응형 감지
queryFn: ...
});