# Pinia๋ž€?

ํ”ผ๋‹ˆ์•„(Pinia) (opens new window)๋ž€ Composition API ๊ธฐ๋ฐ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Vuex์˜ ์ตœ์‹  ๋ฒ„์ „์ธ 5๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

# Vuex๋ž‘ ๋ญ๊ฐ€ ๋‹ค๋ฅผ๊นŒ?

๋ทฐ์—‘์Šค(Vuex)๋Š” Vue.js์˜ ์ƒํƒœ ๊ด€๋ฆฌ ํŒจํ„ด์ด์ž ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ทฐ์—‘์Šค๊ฐ€ state, getters, mutations, actions๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒํƒœ๋ฅผ ๋‹ค๋ค˜๋‹ค๋ฉด ํ”ผ๋‹ˆ์•„๋Š” state, getters, actions๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ทฐ์—‘์Šค์˜ ๋‹ค์†Œ ๋ณต์žกํ•˜๊ฒŒ ๋ณด์ธ ๋ฌธ๋ฒ•๋“ค์ด ๋‹จ์ˆœํ•ด ์ง€๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์žฅ์ ๋งŒ ์žˆ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ํ”ผ๋‹ˆ์•„๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ปดํฌ์ง€์…˜ API ๊ธฐ๋ฐ˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ๋ทฐ์—‘์Šค๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ๋ฒ•์„ ์ตํ˜€์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํ”„๋กœ์ ํŠธ ์„ฑ๊ฒฉ์— ๋งž๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

TIP

๐Ÿ‘‰ ์ด๋ฏธ ์šด์˜ ์ค‘์ธ Vue 2,3 ํ”„๋กœ์ ํŠธ๋Š” Vuex v4.x๋ฅผ ์‚ฌ์šฉ
๐Ÿ‘‰ ์‹ ๊ทœ ํ”„๋กœ์ ํŠธ ์ค‘ Composition API๋งŒ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋Š” Pinia๋ฅผ ์‚ฌ์šฉ

# Pinia์˜ ์ฃผ์š” ์†์„ฑ

ํ”ผ๋‹ˆ์•„๋Š” ๋ทฐ์—‘์Šค์˜ ์ตœ์‹  ๋ฒ„์ „์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ทฐ์—‘์Šค์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ์กด ๋ทฐ์—‘์Šค์˜ ์ฃผ์š” ์†์„ฑ 4๊ฐœ ์ค‘ 3๊ฐœ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

  • state
  • getters
  • actions

๋ทฐ์—‘์Šค์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋˜ mutations๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ  actions ํ•˜๋‚˜๋กœ ์ƒํƒœ ๋ณ€๊ฒฝ๊ณผ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋‘ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ๋Š” ๋‹ค์†Œ ๋ฒˆ๊ฑฐ๋กญ๊ฒŒ ๋Š๊ปด์กŒ๋˜ ๋ฌธ๋ฒ•์ด ๊ฐ„์†Œํ™” ๋˜์—ˆ์ฃ . ๋ทฐ์—‘์Šค์™€ ํ‘œ๋กœ ๋น„๊ตํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Vue Vuex Pinia
data state state
computed getters getters
methods mutations actions
methods actions actions

๋ทฐ ์ธ์Šคํ„ด์Šค์˜ data์™€ state๋Š” ๊ฐ™์€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. data๋Š” ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ์ด๊ณ  state๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์— ๊ณต์œ ๋˜๋Š” data(์ „์—ญ ์ƒํƒœ)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. computed ์†์„ฑ๊ณผ getters ์†์„ฑ ์—ญ์‹œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์—์„œ๋งŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ƒ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ƒ ๋ผ๋Š” ์ฐจ์ด๋งŒ ์žˆ์„ ๋ฟ ์—ญํ• ์€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ methods ์†์„ฑ์€ mutations + actions๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ–ˆ์—ˆ๋Š”๋ฐ ํ”ผ๋‹ˆ์•„์—์„œ๋Š” actions๋งŒ ์œ ์ง€ํ•˜์—ฌ ๋ฌธ๋ฒ•์„ ๊ฐ„์†Œํ™”ํ–ˆ์Šต๋‹ˆ๋‹ค.

# Pinia ์„ค์น˜

ํ”ผ๋‹ˆ์•„๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ Composition API๋กœ๋งŒ ์ž‘์„ฑ๋œ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Vue 2์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Composition API ํ”Œ๋Ÿฌ๊ทธ์ธ (opens new window)์„ ๋ณ„๋„๋กœ ์„ค์น˜ํ•ด์•ผ ํ•˜๊ณ , Vue 3์—์„œ๋Š” ์ปดํฌ์ง€์…˜ API ์Šคํƒ€์ผ ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ปดํฌ์ง€์…˜ API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ทฐ ํ”„๋กœ์ ํŠธ์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ”ผ๋‹ˆ์•„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

# npm
npm install pinia
# yarn
yarn add pinia

# Pinia ๋“ฑ๋ก

ํ”ผ๋‹ˆ์•„๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” main.js ํŒŒ์ผ์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.