diff --git a/docs/React/React Hooks/11. useTransition.md b/docs/React/React Hooks/11. useTransition.md new file mode 100644 index 0000000..9e0cb6f --- /dev/null +++ b/docs/React/React Hooks/11. useTransition.md @@ -0,0 +1,92 @@ +## ๐Ÿง‘๐Ÿปโ€๐Ÿ’ปย useTransition + +--- + +> `useTransition` ์€ UI๋ฅผ ์ฐจ๋‹จํ•˜์ง€ ์•Š๊ณ  state๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋Š” React ํ›…์ž…๋‹ˆ๋‹ค. +> + +### โœ…ย useTransition ๋ฌธ๋ฒ• + +```tsx +const [isPending, startTransition] = useTransition() +``` + +- `useTransition`์€ ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋‹ค. +- `isPending` ์€ ๋ณด๋ฅ˜ ์ค‘์ธ ํŠธ๋žœ์ง€์…˜์ด ์žˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค€๋‹ค. +- `startTransition` ์€ state ์—…๋ฐ์ดํŠธ๋ฅผ ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค. + +## ๐Ÿง‘๐Ÿปโ€๐Ÿ’ปย ์•Œ๊ณ  ๊ฐ€๊ธฐ + +--- + +### โœ…ย useTransition ์‚ฌ์šฉ ํšจ๊ณผ + +- ๋ฆฌ๋ Œ๋”๋ง ๋„์ค‘์—๋„ UI๊ฐ€ ๋ฐ˜์‘์„ฑ์„ ์œ ์ง€ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž๊ฐ€ ํƒญ์„ ํด๋ฆญํ–ˆ๋‹ค๊ฐ€ ๋งˆ์Œ์ด ๋ฐ”๋€Œ์–ด ๋‹ค๋ฅธ ํƒญ์„ ํด๋ฆญํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ฆฌ๋ Œ๋”๋ง์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด ํ•„์š” ์—†์ด ๋‹ค๋ฅธ ํƒญ์„ ํด๋ฆญํ•  ์ˆ˜ ์žˆ๋‹ค. (๋…ผ๋ธ”๋กœํ‚น) +- ๋Š๋ฆฐ ๋””๋ฐ”์ด์Šค์—์„œ๋„ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ์—…๋ฐ์ดํŠธ์˜ ๋ฐ˜์‘์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. + +### โœ…ย `startTransition` ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ• + +```tsx +function TabContainer() { + const [isPending, startTransition] = useTransition(); + const [tab, setTab] = useState('about'); + + function selectTab(nextTab) { + startTransition(() => { + setTab(nextTab); + }); + } + // ... +} +``` + +- state๊ฐ€ ๋กœ๋”ฉ ์ค‘์ผ ๋•Œ `startTransition`ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด state ์—…๋ฐ์ดํŠธ๋ฅผ ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค +- `startTransition` ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•จ์ˆ˜์ธ `scope`๋ฅผ ๋ฐ›๋Š”๋‹ค. +- `scope`๋Š” ๋™๊ธฐ์‹์ด์–ด์•ผ ํ•˜๋ฉฐ, React๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์—†์ดย `scope`๋ฅผ ์ฆ‰์‹œ ํ˜ธ์ถœํ•˜๊ณ ย `scope`ย ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ค‘์— ๋™๊ธฐ์ ์œผ๋กœ ์˜ˆ์•ฝ๋œ ๋ชจ๋“  state ์—…๋ฐ์ดํŠธ๋ฅผ ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค. ์ด๋Š”ย ๋…ผ๋ธ”๋กœํ‚น์ด๊ณ , ๋กœ๋”ฉ์„ ํ‘œ์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค. + +- ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ›์•„์˜จ prop์— state๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋กœ์ง์ด ์žˆ๋‹ค๋ฉด, ํ•ด๋‹น prop์„ `startTransition` ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ๋„ state ์—…๋ฐ์ดํŠธ๋ฅผ ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. +- ํŠธ๋ Œ์ง€์…˜ ์—…๋ฐ์ดํŠธ๋Š” ํ…์ŠคํŠธ ์ž…๋ ฅ(input)์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. + +### โœ…ย ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š”์ง€ + +- ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œ๋œ state์—…๋ฐ์ดํŠธ๋Š” ๋‹ค๋ฅธ state์˜ ์—…๋ฐ์ดํŠธ์— ์˜ํ•ด ์ค‘๋‹จ๋˜๊ธฐ ๋•Œ๋ฌธ์— state ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚  ์‹œ, ๋ฐ”๋กœ ์ฆ‰์‹œ ๊ธฐ์กด ์ด๋ฒคํŠธ์—์„œ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. + +```jsx +import { useTransition } from 'react'; + +export default function TabButton({ children, isActive, onClick }) { + const [isPending, startTransition] = useTransition(); + if (isActive) { + return {children} + } + if (isPending) { + return {children}; + } + return ( + + ); +} +``` + +- isPending ์ƒํƒœ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋กœ๋”ฉ์„ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. +- Suspense๊ฐ€ ๋„์ž…๋œ ๋ผ์šฐํ„ฐ์—์„œ ํŽ˜์ด์ง€ ๋‚ด๋น„๊ฒŒ์ด์…˜์„ ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. + +## ๐Ÿง‘๐Ÿปโ€๐Ÿ’ปย ํ™œ์šฉ ๋ฐ ์ƒ๊ฐํ•  ๊ฑฐ๋ฆฌ + +--- + +### โœ… useTransition๊ณผ ์ผ๋ฐ˜ state ์—…๋ฐ์ดํŠธ์˜ ์ฐจ์ด์  + +**ํŠธ๋žœ์ง€์…˜์—์„œ ํ˜„์žฌ ํƒญ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ** + +state ์—…๋ฐ์ดํŠธ๋Š” ํŠธ๋žœ์ง€์…˜์œผ๋กœ ํ‘œ์‹œ๋˜๋ฏ€๋กœ ๋Š๋ฆฌ๊ฒŒ ๋‹ค์‹œ ๋ Œ๋”๋งํ•ด๋„ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ฉˆ์ถ”์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉ์„ฑ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. + +**ํŠธ๋žœ์ง€์…˜ ์—†์ด ํ˜„์žฌ ํƒญ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ** + +์†๋„๊ฐ€ ๋Š๋ ค์ง„ ํƒญ์„ ๋ Œ๋”๋งํ•˜๋Š” ๋™์•ˆ ์•ฑ์ด ๋ฉˆ์ถ”๊ณ  UI๊ฐ€ ์‘๋‹ตํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. state ์—…๋ฐ์ดํŠธ๋Š” ํŠธ๋žœ์ง€์…˜์ด ์•„๋‹ˆ๋ฏ€๋กœ ๋Š๋ฆฌ๊ฒŒ ๋‹ค์‹œ ๋ Œ๋”๋งํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์ง€๋œ๋‹ค. \ No newline at end of file