๐ ๋ชฉ์ฐจ
- Introduction
- ref
- unref
- toRef
- toRefs
- isRef
- customRef
- shallowRef
- triggerRef
- Conclusion
- Reference
๐ Introduction
์๋ ํ์ธ์. ์ด๋ฒ ํฌ์คํ ์ vue3์ refs์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค. ํด๋น ์ฝ๋๋ ์ฌ๊ธฐ์ํ์ธ ํ์ค์ ์์ต๋๋ค.
๐ ref
ref
inner value๋ก reactiveํ๊ณ mutableํ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ref ๊ฐ์ฒด๋ value property๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
import { ref } from 'vue';
export default {
setup() {
const count = ref(0);
const onClick = () => {
console.log(count);
// {
// __v_isRef: true
// _rawValue: 1
// _shallow: false
// _value: 1
// }
count.value++;
console.log(count.value); // 1
};
return {
onClick,
};
},
};
๐ unref
unref
๋ inner value๋ฅผ ๋ฐํํฉ๋๋ค.
import { ref, unref } from 'vue';
export default {
setup() {
const count = ref(0);
const onClick = () => {
cosnole.log(count.value); // 0
count.value++;
console.log(unref(count)); // 1
};
return {
onClick,
};
},
};
๐ toRef
toRef
๋ reactive ๊ฐ์ฒด์ ๋ํด ref
๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
<script>
import { reactive, toRef } from 'vue';
export default {
setup(props) {
const state = reactive({
foo: 1,
bar: 2,
});
const fooRef = toRef(state, 'foo');
const onClick = () => {
console.log(state); // { foo: 1, bar: 2 }
fooRef.value++;
console.log(state); // { foo: 2, bar: 2 }
};
return {
onClick
};
},
};
</script>
๐ toRefs
toRefs
๋ toRef
์ ๋น์ทํ์ง๋ง ํน์ property๋ฅผ ๋ฐ์ง ์์ต๋๋ค.
import { reactive, toRef, toRefs } from 'vue';
export default {
setup() {
const state = reactive({
foo: 1,
bar: 2,
});
const stateRef = toRefs(state);
const onClick2 = () => {
console.log(state); // { foo: 1, bar: 2 }
stateRef.foo.value++;
stateRef.bar.value++;
console.log(state); // { foo: 2, bar: 3 }
};
return {
onClick2
};
},
};
์ด๋ ๊ฒ๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
// useFeatureX.js
import { reactive, toRefs } from 'vue';
const useFeatureX = () => {
const state = reactive({
foo: 1,
bar: 2
});
return toRefs(state);
};
export default useFeatureX;
// ToRefsPractice.vue
import useFeatureX from '../store/useFeatureX';
export default {
setup() {
const { foo, bar } = useFeatureX();
const onClick = () => {
foo.value++;
bar.value++;
console.log('foo bar', { foo: foo.value, bar: bar.value }); // foo: 2 bar: 3
};
return {
onClick,
};
},
};
๐ isRef
isRef
๋ ํด๋น ๊ฐ์ฒด๊ฐ ref์ธ์ง ์๋์ง๋ฅผ ํ๋ณํด ์ฃผ๋ ํจ์ ์
๋๋ค.
import useFeatureX from '../store/useFeatureX';
import { isRef } from 'vue';
export default {
setup() {
const { foo, bar } = useFeatureX();
const onClick = () => {
console.log('isRef(foo)', isRef(foo)); // true โญ๏ธ
console.log('isRef(bar)', isRef(bar)); // true โญ๏ธ
};
return {
onClick,
};
},
};
๐ customRef
customeRef
๋ ์ธ๋ถ์ ์ผ๋ก ํต์ ๊ถ์ ์ป์ด ์ปจํธ๋กคํ ์ ์์ต๋๋ค. ์์ ๋ก debounce๋ฅผ ์ ์ฉํ ref๋ฅผ ์ ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
import { customRef } from 'vue';
const useDebouncedRef = (value, delay = 200) => {
let timeout;
return customRef((track, trigger) => {
return {
get() {
track();
return value;
},
set(newValue) {
clearTimeout(timeout);
timeout = setTimeout(() => {
value = newValue;
trigger();
}, delay);
}
};
});
};
export default {
setup() {
return {
text: useDebouncedRef('helllo', 1000)
};
}
};
๐ shallowRef
shallowRef
๋ ์ด๋ฆ์์ ์ ์ ์๋ฏ์ด shallowํ ref๋ฅผ ๋ฐํํ๋ ํจ์ ์
๋๋ค.
import { shallowRef, isRef } from 'vue';
export default {
setup() {
const foo = shallowRef({ value: { bar: 2 } });
const onClick = () => {
console.log('isRef(foo)', isRef(foo)); // true โญ๏ธ
console.log('isRef(foo.value)', isRef(foo.value)); // false โญ๏ธ
};
return {
onClick
};
}
};
๐ triggerRef
triggerRef
๋ shallowRef
์ ๊ด๋ จ๋ ๊ฒ์์๋ง ์๋์ผ๋ก watchEffect
์ ๊ฐ์ ํจ๊ณผ๋ฅผ ์ค ์ ์์ต๋๋ค.
import { shallowRef, triggerRef, watchEffect } from 'vue';
export default {
setup() {
const shallow = shallowRef({
greet: 'HELLO'
});
watchEffect(() => {
console.log(shallow.value.greet); // created ๋ ๋๋ง HELLO๊ฐ ์ฐํ๋๋ค.
});
shallow.value.greet = 'HI'; // ์๋ฌด๋ฐ log๊ฐ ์ฐํ์ง ์์ต๋๋ค.
const onClick = () => {
triggerRef(shallow); // triggerRef๋ฅผ ํด์ฃผ์ด์ผ log HI๊ฐ ์ฐํ๋๋ค. โญ๏ธ
};
return {
onClick
};
}
};
๐ Conclusion
์ค๋์ vue3์ ref์ ๊ดํด ์์๋ณด์์ต๋๋ค. ์ด์ ์ฌ์ฌ ํท๊ฐ๋ฆฌ๊ธฐ ์์ํ๋๋ฐ์. ์ค์ ๋ก reactive์ ref๊ฐ ์ด๋ ํ ์ฐจ์ด๋ฅผ ๋ณด์ด๋์ง ์ด๋ป๊ฒ ์ฐ์ด๋์ง์ ๋ํด์๋ ์กฐ๊ธ๋ ๊ณต๋ถ๊ฐ ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค. ๊ทธ๋ผ ๋ค์ ํฌ์คํ ์์ ๋ง๋์~
๐ Reference
'Vue Study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vue3 - teleport (0) | 2020.11.08 |
---|---|
Vue3 - Provide/Inject (0) | 2020.11.04 |
Vue3 - LifeCycle (0) | 2020.10.30 |
Vue3 - computed and watch (0) | 2020.10.28 |
Vue3 - Basic Reactivity APIs (0) | 2020.10.26 |