Zustand Selector ๋ถ์
- ๊ฐ์ฒด selector (shallow ์์)
const { user, theme } = useUserStore((s) => s);
// โ ์คํ ์ด ์ ์ฒด ๊ตฌ๋
. ๊ฐ์ฅ ๋จ์ํ์ง๋ง ๋ชจ๋ ๋ณ๊ฒฝ์ ๋ฆฌ๋ ๋.
- ๊ฐ์ฒด selector (shallow ์์)
const { user, theme } = useUserStore((s) => ({
user: s.user,
theme: s.theme,
}));
// โ ํน์ ๊ฐ๋ง ์ ํํ์ง๋ง, ์ ๊ฐ์ฒด ๋งค๋ฒ ์์ฑ โ ํญ์ ๋ฆฌ๋ ๋.
// ๋ณด๊ธฐ์ ์ข์๋ ์ฑ๋ฅ์์ผ๋ก๋ ์์ข์
- ๊ฐ๋ณ ๊ตฌ๋ (ํ๋๋ณ selector)
const cartItemsMap = useOrderStore((s) => s.cartItemsMap);
const removeFromCart = useOrderStore((s) => s.removeFromCart);
const updateCartItemQuantity = useOrderStore((s) => s.updateCartItemQuantity);
// โ ๊ฐ ํ๋ ๋
๋ฆฝ ๊ตฌ๋
. ํ๋ ๋ฐ๋ ๋๋ง ๋ฆฌ๋ ๋.
- ๊ฐ์ฒด 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 ์ฌ์ฉ์ ์ง์ํด์ผ ํ ๊ฒ ๊ฐ๋ค.