Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Vuejs Composition Function の概要
Search
vuedrink
June 10, 2019
Programming
1
710
Vuejs Composition Function の概要
By Kaoru Hosakawa
From AppSocially KK
At Vue.js Tokyo Drinkup
#vuedrink
vuedrink
June 10, 2019
Tweet
Share
More Decks by vuedrink
See All by vuedrink
Publishing Vue Components to NPM
vuedrink
1
560
Other Decks in Programming
See All in Programming
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
950
Outline View in SwiftUI
1024jp
1
330
最新TCAキャッチアップ
0si43
0
190
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
Macとオーディオ再生 2024/11/02
yusukeito
0
370
みんなでプロポーザルを書いてみた
yuriko1211
0
280
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
930
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
役立つログに取り組もう
irof
28
9.6k
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Become a Pro
speakerdeck
PRO
25
5k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
How GitHub (no longer) Works
holman
310
140k
How to Ace a Technical Interview
jacobian
276
23k
Code Review Best Practice
trishagee
64
17k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Making Projects Easy
brettharned
115
5.9k
Transcript
7VFKT$PNQPTJUJPO'VODUJPOͷ֓ཁ LBPSVIPTPLBXB!DIBUDFOUFSJP "QQ4PDJBMMZגࣜձࣾ
ಈػ
ϩδοΫͷΈཱͯ w 7VFYͰɺίϯϙʔωϯτϕʔεͷ"1*Λ༻͍ͯɺϩδοΫΛΧϓηϧԽ͍ͯ͠Δɻ NJYJOTɺߴ֊ίϯϙʔωϯτɺSFOEFSMFTTίϯϙʔωϯτͳͲͷύλʔϯ͕ଘࡏ͢Δ ͕ɺͦΕͧΕ͕ܽ͋Δɻ w QSPQFSUZͷग़Ͳ͜Ζ͕ෆ໌֬ɺྫ͑ɺͲͷNJYJO͔Βདྷ͍ͯΔͷ͔Θ͔ΓͮΒ͍ w OBNFTQBDFͷিಥɺNJYJOͷϝιου໊ w
ύϑΥʔϚϯεɺߴ֊ίϯϙʔωϯτSFOEFSMFTTίϯϙʔωϯτɺՃͷίϯ ϙʔωϯτ͕ඞཁ w ͦΕʹରͯ͠ɺؔϕʔεͷ"1*ɺ͜ΕΒͷ͕ܽͳ͍ɻϩδοΫΛநग़͠ɺ ʮDPNQPTJUJPOGVODUJPOʯʹ·ͱΊɺϦΞΫςΟϒͳঢ়ଶΛ͢Α͏ʹ͍ͯ͠Δɻ
&YBNQMF function useMouse() { const x = value(0) const y
= value(0) const update = e => { x.value = e.pageX y.value = e.pageY } onMounted(() => { window.addEventListener('mousemove', update) }) onUnmounted(() => { window.removeEventListener('mousemove', update) }) return { x, y } } // in consuming component const Component = { setup() { const { x, y } = useMouse() const { z } = useOtherLogic() return { x, y, z } }, template: `<div>{{ x }} {{ y }} {{ z }}</div>` }
ͦͷଞ w ܕਪ w ͷΰʔϧɿΑΓྑ͍5ZQF4DSJQUܕਪͷαϙʔτ w όϯυϧαΠζ w ؔ"1*ɺ໊લ͖&4FYQPSUTͰΤΩεϙʔτ͞ΕɺΦϯσϚϯυ ͰΠϯϙʔτ͞ΕΔͷͰɺσουίʔυআڈʢUSFFTIBLFʣՄೳ
w ؔ"1*Ϋϥε"1*ΑΓѹॖ͕ߴ͍
ৄࡉ
TFUVQؔ w ίϯϙʔωϯτͷઃఆΛߦ͏ؔ w QSPQTͷ͕ղܾ͠ɺίϯϙʔω ϯτͷΠϯελϯε͕ੜ͞Εͨ ޙɺݺͼग़͞ΕΔ w QSPQTϦΞΫςΟϒ const
MyComponent = { props: { name: String }, setup(props) { console.log(props.name) } }
4UBUF w TFUVQؔɺEBUB ͷΑ͏ʹ UFNQMBUFඳը༻ʹΦϒδΣΫτΛ ฦ͢͜ͱ͕Մೳ w WBMVFXSBQQFSΛͬͯɺTFUVQ ؔͰɺͷมߋ͕ՄೳͳϦΞ ΫςΟϒมΛ࡞Ͱ͖Δʢྫɿ
NTHʣ import { value } from 'vue' const MyComponent = { setup(props) { const msg = value('hello') const appendName = () => { msg.value = `hello ${props.name}` } return { msg, appendName } }, template: `<div @click="appendName">{{ msg }}</div>` }
7BMVF8SBQQFS w +BWB4DSJQUͷϓϦϛςΟϒʢɺจࣈྻͳͲʣɺ͠ɻΑͬͯɺ ؔͷΓ͕มߋͯ͠ߋ৽͞ΕͨΛऔಘ͢Δ͜ͱ͕Ͱ͖ͳ͍ɻ w 7BMVFXSBQQFSΛ͏͜ͱʹΑͬͯҙͷܕΛ࣋ͭΛࢀর͢͠Δ͜ ͱ͕Ͱ͖ΔɻΓɺϦΞΫςΟϒʹͳΓৗʹ࠷৽ʹΛऔಘ͢Δ͜ͱ ͕Մೳɻ
7BMVF6OXSBQQJOH w แ·ΕͨɺSFOEFSίϯςΩ ετϦΞΫςΟϒͳΦϒδΣ ΫτͷೖΕࢠϓϩύςΟ ʢྫɿDPVOUʣͰɺแ͕ղ ͔ΕɺWBMVFͳ͠ͰΞΫηε͢ Δ͜ͱ͕Ͱ͖Δɻ const count
= value(0) const obj = state({ count }) console.log(obj.count) // 0 obj.count++ console.log(obj.count) // 1 console.log(count.value) // 1 count.value++ console.log(obj.count) // 2 console.log(count.value) // 2
$PNQVUFE7BMVFT w $PNQVUFEWBMVFYDPNQVUFE QSPQFSUZͷΑ͏ʹɺґଘ͍ͯ͠Δ Λࢹ͠ɺ͕มߋͨ࣌͠ͷΈ DPNQVUFEWBMVFͷ࠶ܭࢉ͕࣮ࢪ͞ ΕΔɻ w ॻ͖ࠐΈՄೳͳDPNQVUFEWBMVFΛ࡞ Δ߹ɺTFUUFSΛ༻ҙ͢Δɻ
const count = value(0) const writableComputed = computed( // read () => count.value + 1, // write val => { count.value = val - 1 } )
8BUDIFST w XBUDIΛͬͯϦΞΫςΟϒͳε ςʔτΛࢹ͠ɺ෭࡞༻Λى͜͢ ͜ͱ͕Մೳ w ୈ̍Ҿ͕ιʔεʢHFUUFSɺWBMVF ϥούʔɺྻʣɺୈ̎Ҿ͕ ίʔϧόοΫ const
MyComponent = { props: { id: number }, setup(props) { const data = value(null) watch(() => props.id, async (id) => { data.value = await fetchData(id) }) } }
-JGFDZDMF)PPLT w શͯͷMJGFDZDMFIPPLTʹରԠ ͢ΔPO999͕ؔ༻ҙ͞ΕΔ import { onMounted, onUpdated, onUnmounted }
from 'vue' const MyComponent = { setup() { onMounted(() => { console.log('mounted!') }) onUpdated(() => { console.log('updated!') }) onUnmounted(() => { console.log('unmounted!') }) } }
%FQFOEFODZ*OKFDUJPO w ίϯϙʔωϯτ͕QSPWJEF ͨ͠Λมߋͨ࣌͠ɺࢠίϯ ϙʔωϯτߋ৽͕ߦΘΕΔ import { provide, inject }
from 'vue' const CountSymbol = Symbol() const Ancestor = { setup() { // providing a value can make it reactive const count = value(0) provide({ [CountSymbol]: count }) } } const Descendent = { setup() { const count = inject(CountSymbol) return { count } } }
ܽ w ίϯϙʔωϯτఆٛͷϦϑϨΫγϣϯ͓Αͼૢ࡞͕͘͠ͳͬͨ w ͔͠͠ɺϦϑϨΫγϣϯૢ࡞੬͍͠ϦεΩʔͳͷͰٯʹྑ͍͜ͱͳͷ͔͠Εͳ͍ɻؔ "1*ͰΧόʔͰ͖Δͱߟ͍͑ͯΔɻ w ίϯϙʔωϯτΛPQUJPOHSPVQ͝ͱʹ·ͱΊΔඞཁ͕ͳ͍ͨΊɺεύήοςΟίʔυΛॿ͢ Δ w
ίϯϙʔωϯτͷPQUJPOΛ͏ͱཧ͞Ε͍ͯΔΑ͏ʹݟ͑Δ͕ɺϩδοΫ֤PQUJPOʹ ࢄ͞Ε͍ͯΔɻྫ͑ɺσʔλΛऔಘ͢ΔʹɺQSPQTɺEBUB ɺNPVOUFEɺXBUDI͕ඞཁɻ w ؔ"1*ͷ߹ɺҰͭͷؔʹϩδοΫΛ·ͱΊΔ͜ͱ͕Ͱ͖ΔɻڊେͳTFUVQ͕ؔͰ͖ͨ ࣌ɺෳͷؔʹղ͢ΕΑ͍ɻ
ࢀরจݙ rfcs/active-rfcs/0000-function-api