๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

React

React์˜ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, Zustand์— ๋Œ€ํ•œ ๊ฐ„๋žต ์ •๋ฆฌ๊ธ€.

 

https://github.com/pmndrs/zustand

 

 

 

๋…์ผ์–ด๋กœ "์ƒํƒœ"๋ฅผ ์˜๋ฏธํ•˜๋Š” Zustand๋Š” React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋‹จ์ˆœํ™”๋œ Flux ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ์ž‘๊ณ  ๋น ๋ฅธ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

hook์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„ํŽธํ•œ API๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์ตœ์†Œํ•œ์˜ ๋ณด์ผ๋Ÿฌํ”Œ๋ ˆ์ดํŠธ ์ฝ”๋“œ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ์ง๊ด€์ ์ด๊ณ  ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

 

 

 

 

 

์ฃผ์š” ๊ธฐ๋Šฅ ๋ฐ ํŠน์ง•

 

1. ์‹ฌํ”Œํ•œ API:
   Zustand์˜ API๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค. ๋ณดํ†ต 1~2๊ฐœ์˜ hook๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๊ณ , ์ด๋ฅผ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ๋กœ `create` ํ•จ์ˆ˜์™€ `useStore` ํ›…์„ ์‚ฌ์šฉํ•œ๋‹ค.


2. ์ดˆ๊ฒฝ๋Ÿ‰:
   Zustand๋Š” ๊ฒฝ๋Ÿ‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ํฌ๊ธฐ๊ฐ€ ์ž‘๊ณ  ๋น ๋ฅด๋‹ค. React Context API ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ๋‹ค๋ฅธ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ, ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ์ ๋‹ค.


3. ์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ:
   Zustand๋Š” ์ „์—ญ ์ƒํƒœ๋ฅผ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ์ƒํƒœ๋Š” ์ „์—ญ์ ์œผ๋กœ ์ •์˜๋˜๋ฉฐ, ์–ด๋””์„œ๋“  ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค.


4. ๋ฏธ๋“ค์›จ์–ด ์ง€์›:
   Zustand๋Š” ์—ฌ๋Ÿฌ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•ด ์ƒํƒœ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋กœ๊น…, ์ƒํƒœ ์ง€์†์„ฑ, ๋ฐ ํƒ€์ž„ ํŠธ๋ž˜๋ธ” ๋””๋ฒ„๊น… ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.


5. React์˜ ์ƒํƒœ๊ณ„์™€ ํ†ตํ•ฉ:
   Zustand๋Š” React์™€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ†ตํ•ฉ๋œ๋‹ค. React ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๊ณ , ์ด๋ฅผ React ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 

 

๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

 

 

1. zustand ์„ค์น˜

npm i zustand

 



2. ์Šคํ† ์–ด ์ƒ์„ฑ

import { create } from 'zustand'

const useBearStore = create((set) => ({
  bears: 0,
  increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 }),
}))

 



3. ์ปดํฌ๋„ŒํŠธ ๋ฐ”์ธ๋”ฉ์„ ํ•˜๋ฉด ์™„๋ฃŒ.

function BearCounter() {
  const bears = useBearStore((state) => state.bears)
  return <h1>{bears} around here ...</h1>
}

function Controls() {
  const increasePopulation = useBearStore((state) => state.increasePopulation)
  return <button onClick={increasePopulation}>one up</button>
}

 

 

 

 

 

 

๋‹ค์–‘ํ•œ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ

 

- ๋ฏธ๋“ค์›จ์–ด ์ถ”๊ฐ€:
  Zustand๋Š” ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋กœ๊น… ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

  import create from 'zustand'
  import { devtools } from 'zustand/middleware'

  const useStore = create(devtools(set => ({
    bears: 0,
    increasePopulation: () => set(state => ({ bears: state.bears + 1 })),
    removeAllBears: () => set({ bears: 0 })
  })))

 



- Persist:

Zustand๋Š” ์ƒํƒœ๋ฅผ ์Šคํ† ๋ฆฌ์ง€์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ๋„ ์ง€์›ํ•œ๋‹ค.

import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

const useFishStore = create(
  persist(
    (set, get) => ({
      fishes: 0,
      addAFish: () => set({ fishes: get().fishes + 1 }),
    }),
    {
      name: 'food-storage', // name of the item in the storage (must be unique)
      storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
    },
  ),
)

 

 

 

์ด ์™ธ์—๋„, ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ง€์›ํ•œ๋‹ค. 

 

 

 

 

์ด๋ฒˆ ๊ธฐํšŒ์— hook์„ ์ด์šฉํ•œ ์ƒํƒœ๊ด€๋ฆฌ ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด ์•„๋ž˜์˜ ๋„์„œ๋ฅผ ๊ตฌ๋งคํ–ˆ๋‹ค. ๋„์„œ๋กœ ๊ณต๋ถ€ํ•œ ๋’ค, ์‹ฌํ™”๊ณผ์ •๋„ ์ •๋ฆฌํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค.

 

 

 


 

๐ŸŒธ ์ถœ์ฒ˜ ๐ŸŒธ

 

Zustand

pmndrs/zustand(git)