ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฏ useSyncExternalStore๋ ๋ฌด์์ผ๊น์?
**useSyncExternalStore**๋ **์ธ๋ถ ์ ์ฅ์(๋ฐ์ดํฐ)**์ ์ํ๋ฅผ React ์ปดํฌ๋ํธ์ ๋๊ธฐํ์ํฌ ๋ ์ฌ์ฉํ๋ ํ ์ด์์.
์ด ํ
์ ์ฃผ๋ก ์ธ๋ถ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ(์: Redux, Zustand ๋ฑ)๋ฅผ React ์ปดํฌ๋ํธ์ ํจ๊ป ์ฌ์ฉํ ๋ ์ ์ฉํด์.
์ธ๋ถ ์ ์ฅ์์์ ๊ฐ์ด ๋ฐ๋๋ฉด ํ๋ฉด์ ์ฆ์ ๋ฐ์๋๋๋ก ๋์์ฃผ๋ ์ญํ ์ ํด์.
๐ ์ค์ํ ๋น์ : ๋ด์ค ์๋ฆผํ
๋๋ค์ ๋ด์ค ์๋ฆผํ์ด ํ๋ ์๋ค๊ณ ์๊ฐํด ๋ด์. ๐ฐ
- ์ด ์๋ฆผํ์ ์ธ๋ถ ์ ์ฅ์์ฒ๋ผ ์ฌ๋ฌ ๊ณณ์์ ๊ด๋ฆฌ๋ผ์.
- ์ฌ๋๋ค์ด ์๋ฆผํ์ ๋ด์ฉ์ ๋ณด๊ณ ์ต์ ์์์ ํ์ธํ์ฃ .
- ๋ด์ค๊ฐ ๋ฐ๋๋ฉด ์๋ฆผํ์ ์ฆ์ ๋ฐ์๋๊ณ , ์ฌ๋๋ค์ ์ ์์์ ํ์ธํ๊ฒ ๋ฉ๋๋ค.
์ฌ๊ธฐ์ **useSyncExternalStore**๋ ์๋ฆผํ์ ์ค์๊ฐ์ผ๋ก ํ์ธํ๊ณ , ๋ด์ฉ์ด ๋ฐ๋๋ฉด ํ๋ฉด์ ๋ฐ๋ก ๋ณด์ฌ์ฃผ๋ ์ญํ ์ ํด์!
๐ ๏ธ useSyncExternalStore ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
React ์ฝ๋๋ก ์์๋ฅผ ๋ง๋ค์ด ๋ณผ๊ฒ์.
์ธ๋ถ ์ ์ฅ์์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ณ , ๋ฐ์ดํฐ๊ฐ ๋ฐ๋๋ฉด ์ฆ์ ํ๋ฉด์ ์
๋ฐ์ดํธํ๋ ์์ ์
๋๋ค.
1. ๊ฐ๋จํ ์ธ๋ถ ์ ์ฅ์ ๋ง๋ค๊ธฐ
// ์ธ๋ถ ์ ์ฅ์ (store)๋ฅผ ๋ง๋ญ๋๋ค.
const store = {
value: 0, // ์ด๊ธฐ ์ํ
listeners: new Set(), // ์ํ ๋ณ๊ฒฝ ์ ์คํํ ๋ฆฌ์ค๋๋ค
// ์ํ๋ฅผ ๊ตฌ๋
ํ๋ ํจ์
subscribe(listener) {
this.listeners.add(listener);
return () => this.listeners.delete(listener); // ๊ตฌ๋
ํด์
},
// ์ํ๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์
getSnapshot() {
return this.value;
},
// ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ํจ์
setValue(newValue) {
this.value = newValue;
this.listeners.forEach((listener) => listener()); // ๋ชจ๋ ๋ฆฌ์ค๋ ์คํ
},
};
export default store;
2. useSyncExternalStore๋ก ์ธ๋ถ ์ํ ์ฌ์ฉํ๊ธฐ
import React, { useSyncExternalStore } from "react";
import store from "./store";
function Counter() {
// ์ธ๋ถ ์ ์ฅ์์ ์ํ๋ฅผ ๊ตฌ๋
ํฉ๋๋ค.
const value = useSyncExternalStore(
store.subscribe, // ์ํ๊ฐ ๋ฐ๋ ๋ ์คํ๋ ๊ตฌ๋
ํจ์
store.getSnapshot // ํ์ฌ ์ํ ๊ฐ์ ๊ฐ์ ธ์ค๋ ํจ์
);
const increment = () => {
store.setValue(value + 1); // ์ธ๋ถ ์ํ๋ฅผ ์
๋ฐ์ดํธ
};
return (
<div>
<h1>์นด์ดํฐ: {value}</h1>
<button onClick={increment}>+1 ์ฆ๊ฐ</button>
</div>
);
}
export default Counter;
๐ ์ฝ๋ ์ค๋ช
- ์ธ๋ถ ์ ์ฅ์ (store)
- store๋ ๊ฐ(value)์ ๊ฐ์ง๊ณ ์๊ณ , ๊ตฌ๋ (subscribe)๊ณผ ์ํ ์กฐํ(getSnapshot) ๊ธฐ๋ฅ์ ์ ๊ณตํด์.
- setValue๋ฅผ ํธ์ถํ๋ฉด ๊ฐ์ด ๋ฐ๋๊ณ , ๊ตฌ๋ ํ ๋ฆฌ์ค๋๋ค์ด ์คํ๋ฉ๋๋ค.
- useSyncExternalStore ์ฌ์ฉ
- useSyncExternalStore๋ฅผ ํตํด ์ธ๋ถ ์ ์ฅ์๋ฅผ React ์ปดํฌ๋ํธ์ ์ฐ๊ฒฐํด์.
- store.subscribe๋ ์ํ๊ฐ ๋ณ๊ฒฝ๋ ๋ ์คํ๋ ๊ตฌ๋ ํจ์์ ๋๋ค.
- store.getSnapshot์ ํ์ฌ ์ํ ๊ฐ์ ๊ฐ์ ธ์ค๋ ํจ์์ ๋๋ค.
- ์ํ ์
๋ฐ์ดํธ
- ๋ฒํผ ํด๋ฆญ ์ store.setValue๋ฅผ ํธ์ถํด์ ์ธ๋ถ ์ ์ฅ์์ ๊ฐ์ ๋ฐ๊ฟ๋๋ค.
- ๊ฐ์ด ๋ฐ๋๋ฉด useSyncExternalStore๊ฐ ์์์ ํ๋ฉด์ ๋ค์ ๊ทธ๋ ค์ค์.
๐งฉ ์ธ์ useSyncExternalStore๋ฅผ ์ฌ์ฉํ ๊น์?
- ์ธ๋ถ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ๊ป ์ฌ์ฉํ ๋
- Redux, Zustand, MobX ๊ฐ์ ์ธ๋ถ ์ํ ๊ด๋ฆฌ ๋๊ตฌ์ React๋ฅผ ์ฐ๊ฒฐํ ๋ ์ ์ฉํด์.
- ์ค์๊ฐ ๋ฐ์ดํฐ ์
๋ฐ์ดํธ
- ์ธ๋ถ์์ ์ํ๊ฐ ๋ฐ๋๋ฉด React ์ปดํฌ๋ํธ๋ ๋ฐ๋ก ๋ฐ์ํด์ผ ํ ๋ ์ฌ์ฉํด์.
- ์ฑ๋ฅ ์ต์ ํ
- useSyncExternalStore๋ ์ํ ๋ณํ๋ฅผ ์ต์ ํ๋ ๋ฐฉ์์ผ๋ก ๊ฐ์งํด์ React ๋ ๋๋ง์ ์ต์ํํด์.
๐ ํ ์ค ์์ฝ
useSyncExternalStore๋ ์ธ๋ถ ์ ์ฅ์์ ์ํ๋ฅผ React ์ปดํฌ๋ํธ์ ์ค์๊ฐ์ผ๋ก ๋๊ธฐํํ๋ ํ ์ด์์.
'web > react hook' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
useTransition (0) | 2024.12.14 |
---|---|
useState (0) | 2024.12.14 |
useRef (0) | 2024.12.14 |
useReducer (0) | 2024.12.14 |
useOptimistic (0) | 2024.12.14 |
- Total
- Today
- Yesterday
- ๋ฒ ํ
- ์ต์ ๋ฐ์ดํฐ
- Store
- react
- useRef
- state
- ์ธ๋ถํฐํธ
- experimental_taintobjectreference
- experimental
- experimental_taintuniquevalue
- react dom
- finddomnode
- ์ ์ฅ์
- ์คํ์
- Props
- useid
- usedeferredvalue
- dns-prefetch
- flushsync
- react hook
- react dom hook
- useinsertioneffect
- zustand
- useformstatus
- ๋ฐ์ดํฐ์ ์ง
- ์ํ๊ด๋ฆฌ
- component
- prefetchdns
- Nextjs
- preconnect
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |