Zustand - woowa-techcamp-2021/store-4 GitHub Wiki

https://velog.io/@augusty/Zustand-๋ฆฌ์•กํŠธ์—์„œ์˜-Redux-์–ด๋ ต๊ฒŒ-์“ฐ์ง€๋งˆ์„ธ์š”

https://github.com/pmndrs/zustand

  • redux ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ๋‹ค.
  • redux ๋ž‘ ๋น„์Šทํ•˜๋‹ค.
    • (Redux devtools ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.)
  • ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ Provider ๋กœ ๊ฐ์Œ€ ํ•„์š”๊ฐ€ ์—†๋‹ค.
import create from 'zustand'

const [useStore] = create(set => ({
  count: 1,
  inc: () => set(state => ({ count: state.count + 1 })),
  dec: () => set(state => ({ count: state.count - 1 }))
}))

function Counter() {
	// state ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„.
  const count = useStore(state => state.count)
  return <span>{count}</span>
}

function Controls() {
	// ํ˜ธ์ถœ๋˜์–ด state ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ถ€๋ถ„.
  const { inc, dec } = useStore()
  return (
    <>
      <button onClick={inc}>up</button>
      <button onClick={dec}>down</button>
    </>
  )
}
  • ์žฅ์  : ๊ฐ„๋‹จํ•˜๋‹ค.

    • store ์˜ state ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•œ๋‹ค. (โ‡’ props ์— ๋งคํ•‘ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.)
    • action ์„ ์ •์˜ํ•  ํ•„์š” X.
    • ๊ทธ๋ž˜์„œ action ์„ ๊ตฌ๋ถ„ํ•  switch case ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” X.
  • ๋‹จ์  : ํ•œ๊ธ€ ์ž๋ฃŒ๊ฐ€ ๋„ˆ๋ฌด ์—†๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ