🎨FrontEnd/Vue

vue 3 λ°˜μ‘ν˜• - reactive and ref

IOTrue 2023. 7. 5. 22:45

😎 vue 3 λ°˜μ‘ν˜• - reactive and ref

composition APIλ₯Ό κΈ°μ€€μœΌλ‘œ ν•©λ‹ˆλ‹€

 

λ°˜μ‘ν˜•
vueμ—μ„œλŠ” λ°μ΄ν„°μ˜ μ‹€μ‹œκ°„ μ—…λ°μ΄νŠΈκ°€ κ°€λŠ₯ν•œ 것을 λ°˜μ‘ν˜•μ΄λΌκ³  ν•œλ‹€

βœ…  reactive

reactive() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 객체 λ˜λŠ” 배열을 λ°˜μ‘ν˜•μœΌλ‘œ λ§Œλ“€ 수 μžˆλ‹€

λ°˜ν™˜ 값은 원본 객체와 κ°™μ§€ μ•Šκ³  원본 객체λ₯Ό μž¬μ •μ˜ν•œ 객체이닀

const raw = {}
const proxy = reactive(raw)
// λ°˜μ‘ν˜•μœΌλ‘œ μž¬μ •μ˜ 된 것은 원본과 κ°™μ§€ μ•Šλ‹€
console.log(proxy === raw) // false

객체, λ°°μ—΄ λ˜λŠ” Mapμ΄λ‚˜ Setκ³Ό 같은 μ»¬λ ‰μ…˜ μœ ν˜•μ—λ§Œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€

 

reactive 예제

μˆ«μžκ°€ μ¦κ°€ν•˜λ©° 화면에 μ‹€μ‹œκ°„ 바인딩을 ν•˜λŠ” μ˜ˆμ œμ΄λ‹€

reactive μ‚¬μš© 여뢀에 λ”°λ₯Έ 차이점을 λΉ„κ΅ν•΄λ³΄μž

<div>{{ state.count }}</div>
<button @click="state.count++">state.count++</button>
<div>{{ countNormal }}</div>
<button @click="countNormal++">countNormal++</button>


import { reactive } from "vue"

const countNormal = 0
const state = reactive({ count: 0 })

reactiveλ₯Ό μ‚¬μš©ν•œ state.count의 μ‹€μ‹œκ°„ λ°”μΈλ”©λ§Œ κ°€λŠ₯ν•˜λ‹€

μ΄λ ‡κ²Œ μ‹€μ‹œκ°„ 바인딩이 κ°€λŠ₯ν•œ 것을 vueμ—μ„œλŠ” λ°˜μ‘ν˜•μ΄λΌκ³  ν•œλ‹€

 

reactive()λŠ” 예제λ₯Ό 보면 μ•Œ 수 μžˆλ“―μ΄ state.count처럼 νŠΉμ • 값에 μ ‘κ·Όν•  λ•Œμ—λŠ” λ°μ΄ν„°μ˜ μ†μ„±μœΌλ‘œ μ ‘κ·Όν•΄μ•Όν•œλ‹€

 

βœ…  ref

ref()λŠ” reactive의 μ œν•œ 사항을 ν•΄κ²°ν•˜κΈ° μœ„ν•΄, νƒ€μž… μ œν•œ 없이 데이터λ₯Ό λ°˜μ‘ν˜•μœΌλ‘œ μž¬μ •μ˜ν•  수 μžˆλŠ” ν•¨μˆ˜μ΄λ©°

.value μ†μ„±μ„ ν¬ν•¨ν•˜λŠ” ref κ°μ²΄λ‘œ λ°˜ν™˜ν•œλ‹€

const countRef = ref(0)
console.log(countRef) // { value: 0 }
console.log(countRef.value) // 0
countRef.value++
console.log(countRef.value) // 1

 

 

ref 예제

reactive() μ˜ˆμ œμ™€ 같이 'μˆ«μžκ°€ μ¦κ°€ν•˜λ©° 화면에 μ‹€μ‹œκ°„ 바인딩을 ν•˜λŠ” 예제'λ₯Ό ν†΅ν•΄μ„œ

reactive와 ref의 문법 차이점을 λΉ„κ΅ν•΄λ³΄μž

<div>{{ stateReactive.count }}</div>
<button @click="stateReactive.count++">stateReactive.count++</button>
<div>{{ countRef }}</div>
<button @click="countRef++">countRef++</button>


import { reactive, ref } from "vue"

const stateReactive = reactive({ count: 0 })
console.log(stateReactive.count)

const countRef = ref(0)
console.log(countRef.value)

template에 바인딩을 ν•  λ•Œ refλŠ” 속성이 μ•„λ‹Œ ν•΄λ‹Ή 데이터에 λ°”λ‘œ μ ‘κ·Όν•  수 있기 λ•Œλ¬Έμ— reactive에 λΉ„ν•΄ κ°„λ‹¨ν•˜λ‹€

ν•˜μ§€λ§Œ refλŠ” script μ˜μ—­μ—μ„œ ν•΄λ‹Ή 데이터에 μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” .valueλ₯Ό ν†΅ν•΄μ„œ μ ‘κ·Όν•΄μ•Ό ν•œλ‹€

 

βœ…  reactive vs ref

  • reactive()와 ref() λͺ¨λ‘ μƒˆλ‘œμš΄ 객체λ₯Ό λ°˜ν™˜ν•œλ‹€
  • reactiveλŠ” 객체와 λ°°μ—΄ λ˜λŠ” μ»¬λ ‰μ…˜ μœ ν˜•λ§Œ λ°˜μ‘ν˜•μœΌλ‘œ μž¬μ •μ˜ν•  수 μžˆλ‹€
  • refλŠ” νƒ€μž…μ œν•œ 없이 μ‚¬μš© κ°€λŠ₯ν•˜λ‹€

reactive와 refλ₯Ό ν˜Όμš©ν• μ§€ ν•˜λ‚˜λ§Œμ„ μ‚¬μš©ν• μ§€μ— λŒ€ν•œ μ»¨λ²€μ…˜μ„ μ •μ˜ν•œ ν›„ ν•΄λ‹Ή μ»¨λ²€μ…˜μ„ 따라가면 λœλ‹€

 

 

βœ…  λž˜ν•‘, μ–Έλž˜ν•‘

μ–΄λ–€ 값이 ref에 ν• λ‹Ήλ˜μ–΄ μžˆλ‹€κ³  κ°€μ •ν•œλ‹€

이 λ•Œ, .valueλ₯Ό 톡해 ν• λ‹Ήλœ 값에 μ ‘κ·Όν•΄μ•Ό ν•œλ‹€λ©΄ "λž˜ν•‘"된 것이고 .value μ—†μ΄ μ ‘κ·Όλœλ‹€λ©΄ "μ–Έλž˜ν•‘"된 것이닀

μ•„λž˜ 예제λ₯Ό 보면 script μ˜μ—­μ—μ„œλŠ” λž˜ν•‘λ˜μ–΄ 있고 template μ˜μ—­μ—μ„œλŠ” μ–Έλž˜ν•‘λ˜μ–΄ μžˆλ‹€

<div>{{ countRef }}</div>
<button @click="countRef++">countRef++</button>


import { ref } from "vue"

const countRef = ref(0)
console.log(countRef.value)

 

 

 

https://v3-docs.vuejs-korea.org/guide/essentials/reactivity-fundamentals.html

 

λ°˜μ‘ν˜• 기초 | Vue.js

 

v3-docs.vuejs-korea.org