Zustand Selector ๋ถ„์„

  1. ๊ฐ์ฒด selector (shallow ์—†์Œ)
const { user, theme } = useUserStore((s) => s);
// โ†’ ์Šคํ† ์–ด ์ „์ฒด ๊ตฌ๋…. ๊ฐ€์žฅ ๋‹จ์ˆœํ•˜์ง€๋งŒ ๋ชจ๋“  ๋ณ€๊ฒฝ์— ๋ฆฌ๋ Œ๋”.
  1. ๊ฐ์ฒด selector (shallow ์—†์Œ)
const { user, theme } = useUserStore((s) => ({
  user: s.user,
  theme: s.theme,
}));
// โ†’ ํŠน์ • ๊ฐ’๋งŒ ์„ ํƒํ•˜์ง€๋งŒ, ์ƒˆ ๊ฐ์ฒด ๋งค๋ฒˆ ์ƒ์„ฑ โ†’ ํ•ญ์ƒ ๋ฆฌ๋ Œ๋”.
// ๋ณด๊ธฐ์—” ์ข‹์•„๋„ ์„ฑ๋Šฅ์ƒ์œผ๋กœ๋Š” ์•ˆ์ข‹์Œ
  1. ๊ฐœ๋ณ„ ๊ตฌ๋… (ํ•„๋“œ๋ณ„ selector)
const cartItemsMap = useOrderStore((s) => s.cartItemsMap);
const removeFromCart = useOrderStore((s) => s.removeFromCart);
const updateCartItemQuantity = useOrderStore((s) => s.updateCartItemQuantity);
// โ†’ ๊ฐ ํ•„๋“œ ๋…๋ฆฝ ๊ตฌ๋…. ํ•„๋“œ ๋ฐ”๋€” ๋•Œ๋งŒ ๋ฆฌ๋ Œ๋”.
  1. ๊ฐ์ฒด selector + shallow ๋น„๊ต
const { user, theme } = useUserStore(useShallow(
  (s) => ({
   user: s.user, 
   theme: s.theme
    })));
// ๊ฐ์ฒด ๋ฆฌํ„ด์ด์ง€๋งŒ ์–•์€ ๋น„๊ต๋กœ ๋ฆฌ๋ Œ๋” ์ตœ์†Œํ™”.

๋น„๊ต ๋ฉ”์„œ๋“œ : shallow

https://zustand.docs.pmnd.rs/apis/shallow

๋‚ด๋ถ€์ ์œผ๋กœ ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

Comparing Primitives

const stringLeft = 'John Doe'
const stringRight = 'John Doe'

Object.is(stringLeft, stringRight) // -> true
shallow(stringLeft, stringRight) // -> true

const numberLeft = 10
const numberRight = 10

Object.is(numberLeft, numberRight) // -> true
shallow(numberLeft, numberRight) // -> true

const booleanLeft = true
const booleanRight = true

Object.is(booleanLeft, booleanRight) // -> true
shallow(booleanLeft, booleanRight) // -> true

const bigIntLeft = 1n
const bigIntRight = 1n

Object.is(bigIntLeft, bigIntRight) // -> true
shallow(bigIntLeft, bigIntRight) // -> true
  • ๊ณต์‹๋ฌธ์„œ์— ๋‚˜์˜จ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
    • primitive ๋น„๊ต์—์„œ๋Š” actual value๊ฐ€ ๊ฐ™์œผ๋ฉด ํ•ญ์ƒ true๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.

Comparing Objects

// -------------๊ฐ์ฒด ๋น„๊ต--------------
const objectLeft = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
}
const objectRight = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
}

Object.is(objectLeft, objectRight) // -> false
shallow(objectLeft, objectRight) // -> true


// -------------Set ๋น„๊ต--------------
const setLeft = new Set([1, 2, 3])
const setRight = new Set([1, 2, 3])

Object.is(setLeft, setRight) // -> false
shallow(setLeft, setRight) // -> true


// -------------Maps ๋น„๊ต--------------
const mapLeft = new Map([
  [1, 'one'],
  [2, 'two'],
  [3, 'three'],
])
const mapRight = new Map([
  [1, 'one'],
  [2, 'two'],
  [3, 'three'],
])

Object.is(mapLeft, mapRight) // -> false
shallow(mapLeft, mapRight) // -> true
  • ๊ฐ์ฒด, set, Maps ์•ˆ์— ๋‹ด๊ธฐ๋Š” ๋‚ด์šฉ์ด ๊ฐ™๋”๋ผ๋„
    • Object.is() ๋ฉ”์„œ๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ๋น„๊ตํ•˜๋ฉด false
    • shallow() ๋ฉ”์„œ๋“œ๋กœ ๋น„๊ตํ•˜๋ฉด true

Comparing Complicated Objects

  • ํ•˜์ง€๋งŒ ๊ฐ์ฒด ๋‚ด๋ถ€์— ๋˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‚ด๋ถ€ ๊ฐ’๋“ค์ด ๊ฐ™๋”๋ผ๋„ ํ•ญ์ƒ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const objectLeft = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
  address: {
    street: 'Kulas Light',
    suite: 'Apt. 556',
    city: 'Gwenborough',
    zipcode: '92998-3874',
    geo: {
      lat: '-37.3159',
      lng: '81.1496',
    },
  },
}
const objectRight = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
  address: {
    street: 'Kulas Light',
    suite: 'Apt. 556',
    city: 'Gwenborough',
    zipcode: '92998-3874',
    geo: {
      lat: '-37.3159',
      lng: '81.1496',
    },
  },
}

Object.is(objectLeft, objectRight) // -> false
shallow(objectLeft, objectRight) // -> false

์•„์ง ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ๋งˆ์ฃผํ•ด ๋ณธ ์ ์€ ์—†์ง€๋งŒ ๋งˆ์ฃผํ•œ๋‹ค๋ฉด

  • ๊ฐ์ฒด ๊ตฌ์กฐ๋ฅผ ๋‹จ์ˆœํ™” ( ๋ށ์Šค๋ฅผ ๋‚ฎ์ถ”๊ณ  flatten ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ )
  • ๊ฐ์ฒด ๊ตฌ์กฐ๊ฐ€ ์–ด์ฉ” ์ˆ˜ ์—†์ด ๋ณต์žกํ•œ ๊ฒฝ์šฐ์—๋Š” useShallow ์‚ฌ์šฉ์„ ์ง€์–‘ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.