๐ ๋ชฉ์ฐจ
- Introduction
- computed
- watchEffect
- watch
- Conclusion
- Reference
๐ Introduction
์๋ ํ์ธ์. ์ด๋ฒ ํฌ์คํ ์์๋ computed์ watch์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค. ํด๋น ์ฝ๋๋ ์ฌ๊ธฐ์ํ์ธํ ์ ์์ต๋๋ค.
๐ computed
computed
๋ ๊ธฐ์กด vue 2์์์ computed์ ๊ฐ์ต๋๋ค. ๋ค๋ฅธ ์ ์ immutable ref object๋ฅผ ๋ฐํํฉ๋๋ค.
import { ref, computed } from 'vue';
export default {
setup() {
const count = ref(1);
const plusOne = computed(() => count.value + 1);
const onClick = () => {
console.log('count', count.value); // 1
console.log('plusOne', plusOne.value); // 2
};
return {
onClick
};
}
};
get๊ณผ set์ผ๋ก ์ฌ์ฉ๋ ๊ฐ๋ฅํฉ๋๋ค.
import { ref, computed } from 'vue';
export default {
setup() {
const count = ref(1);
const plustTwo = computed({
get: () => count.value + 2,
set: val => {
count.value = val;
}
});
const onClick = () => {
console.log('count', count.value); // 1
console.log('plusTwo', plustTwo.value); // 3;
plustTwo.value = 0;
console.log('plusTwo', plustTwo.value); // 2
};
return {
onClick
};
}
};
๐ watchEffect
watchEffect
๋ reactive๋ฅผ ๊ด์ฐฐํ์ฌ ๋ณ๊ฒฝ์ด ์ผ์ด๋๋ฉด ๋ด๋ถ์ ํจ์๋ฅผ ์คํ ์ํต๋๋ค.
import { watchEffect, ref } from 'vue';
export default {
setup() {
const count = ref(0);
watchEffect(() => console.log(count.value)); // created ๋ ๋ log 0 โญ๏ธ
let timeout;
const onClick = () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
count.value++; // console.log(1) by watchEffect โญ๏ธ
}, 1000);
};
return {
onClick
};
}
};
๐ watch
watch
๋ ๊ธฐ์กด vue2์์์ watch์ ๊ฐ์ต๋๋ค. ๊ทธ๋ฌ๋ reactive์ ref๋ฅผ ์ ์ธํด์ฃผ๋๊ฒ ์กฐ๊ธ ๋ค๋ฆ
๋๋ค.
reactive ์ผ๋:
import { watch, reactive } from 'vue';
export default {
setup() {
const state = reactive({ count: 0 });
watch(
() => state.count,
(count, prevCount) => {
console.log('count', count);
console.log('prevCount', prevCount);
}
);
const onClick = () => {
state.count++; // count 1 prevCount 2 โญ๏ธ
};
return {
onClick
};
}
};
ref์ผ๋:
import { watch, ref } from 'vue';
export default {
setup() {
const count = ref(0);
watch(count, (count, prevCount) => {
console.log('count', count);
console.log('prevCount', prevCount);
});
const onClick = () => {
count.value++; // count 1 prevCount 2
};
return {
onClick
};
}
};
๋ํ ๋ค์ค์ผ๋ก watch๋ฅผ ํด์ค์ ๋ ์์ต๋๋ค.
import { watch, ref } from 'vue';
export default {
setup() {
const count1 = ref(0);
const count2 = ref(0);
watch([count1, count2], ([count1, count2], [prevCount1, prevCount2]) => {
console.log('count1', count1);
console.log('prevCount1', prevCount1);
console.log('count2', count2);
console.log('prevCount2', prevCount2);
});
const onClick = () => {
count1.value++; // count1 1 prevCount1 2 count2 0 prevCount2 0
};
return {
onClick
};
}
};
๐ Conclusion
computed์ watch๋ reactive์ ref์ ๋นํด ์ด๋ ต์ง๋ ์์๋๊ฑฐ ๊ฐ์ต๋๋ค. ์ค์ ๋ก ๋ ํ์ธ์ ํด๋ด์ผ๊ฒ ์ง๋ง ๋ค์ค์ผ๋ก watch๋ฅผ ์ธ์ง๋ ์กฐ๊ธ ์๋ฌธ์ด ๋ญ๋๋ค. ๊ทธ๋ผ ๋ค์ ํฌ์คํ ์์ ๋ง๋์~~
๐ Reference
'Vue Study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vue3 - teleport (0) | 2020.11.08 |
---|---|
Vue3 - Provide/Inject (0) | 2020.11.04 |
Vue3 - LifeCycle (0) | 2020.10.30 |
Vue3 - Refs (0) | 2020.10.27 |
Vue3 - Basic Reactivity APIs (0) | 2020.10.26 |