Notice
Recent Posts
Recent Comments
Link
SeouliteLab
[Vue.js] onRenderTracked 훅을 활용한 렌더링 추적 본문
Vue.js에서는 컴포넌트의 상태 변화를 추적하기 위해 onRenderTracked
훅을 제공합니다. 이 훅은 컴포넌트의 렌더링 과정에서 추적할 수 있는 상태 변화를 감지하여 작업을 수행할 때 사용됩니다. 주로 특정 데이터나 상태의 변경을 모니터링하고 디버깅할 때 활용됩니다.
아래는 onRenderTracked
훅을 사용한 Vue 컴포넌트의 예제입니다.
<template>
<div>
<p>{{ message }}</p>
<button @click="updateMessage">메시지 업데이트</button>
</div>
</template>
<script>
import { ref, onRenderTracked } from 'vue';
export default {
setup() {
const message = ref('초기 메시지');
function updateMessage() {
message.value = '새로운 메시지';
}
onRenderTracked((target, key) => {
// 렌더링 추적이 시작될 때 호출되는 함수
console.log(`렌더링 추적이 시작되었습니다. 대상: ${target}, 키: ${key}`);
});
return {
message,
updateMessage
};
}
};
</script>
예제 설명
위 예제는 "메시지 업데이트" 버튼을 클릭하여 메시지를 업데이트하는 Vue 컴포넌트입니다.
message
변수는 컴포넌트에서 사용될 메시지를 담고 있습니다.updateMessage
메소드는 버튼을 클릭하여 새로운 메시지로 업데이트합니다.onRenderTracked
훅은 렌더링 추적이 시작될 때 호출되는 함수를 정의합니다. 여기에서는 추적 대상과 키를 콘솔에 출력합니다.
onRenderTracked
훅을 사용하면 Vue 컴포넌트의 렌더링 과정에서 특정 상태 변화를 추적할 수 있습니다. 이를 통해 애플리케이션의 상태 변화를 디버깅하고 모니터링하는 데 도움이 됩니다.