startTransition์€ UI๋ฅผ Blocking ์•Š๊ณ  ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

startTransition(scope)

Reference

startTransition(scope)

startTransition ํ•จ์ˆ˜๋Š” ์ƒํƒœ ์—…๋ฐ์ดํŠธ๋ฅผ transition์œผ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

import { startTransition } from 'react';

function TabContainer() {
const [tab, setTab] = useState('about');

function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
// ...
}

See more examples below.

๋งค๊ฐœ๋ณ€์ˆ˜

  • scope: ํ•˜๋‚˜ ์ด์ƒ์˜ set ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ผ๋ถ€ state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. React๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์—†์ด scope๋ฅผ ์ฆ‰์‹œ ํ˜ธ์ถœํ•˜๊ณ  scope ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋™์•ˆ ๋™๊ธฐ์ ์œผ๋กœ ์˜ˆ์•ฝ๋œ ๋ชจ๋“  state ์—…๋ฐ์ดํŠธ๋ฅผ transition์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” non-blocking์ด๋ฉฐ ์›์น˜ ์•Š๋Š” ๋กœ๋”ฉ์„ ํ‘œ์‹œํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ ๊ฐ’

startTransition์€ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ

  • startTransition์€ transition์ด ์ง„ํ–‰ ์ค‘์ธ์ง€ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ง„ํ–‰ ์ค‘์ธ transition์„ ํ‘œ์‹œํ•˜๋ ค๋ฉด useTransition์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  • ํ•ด๋‹น state์˜ set ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ๋ฅผ transition์œผ๋กœ ๋ž˜ํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ props์ด๋‚˜ custom Hook ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ transition์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด useDeferredValue๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•˜์„ธ์š”.

  • startTransition์— ์ „๋‹ฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋™๊ธฐ์‹์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. React๋Š” ์ด ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ๋™์•ˆ ๋ฐœ์ƒํ•˜๋Š” ๋ชจ๋“  state ์—…๋ฐ์ดํŠธ๋ฅผ transition์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๋” ๋งŽ์€ state ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ํ•˜๋ฉด(์˜ˆ์‹œ: timeout), transition์œผ๋กœ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • Transition์œผ๋กœ ํ‘œ์‹œ๋œ state ์—…๋ฐ์ดํŠธ๋Š” ๋‹ค๋ฅธ state ์—…๋ฐ์ดํŠธ์— ์˜ํ•ด ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, transition ๋‚ด์—์„œ ์ฐจํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์ง€๋งŒ ์ฐจํŠธ๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง๋˜๋Š” ๋™์•ˆ ์ž…๋ ฅ์„ ์‹œ์ž‘ํ•˜๋ฉด React๋Š” ์ž…๋ ฅ state ์—…๋ฐ์ดํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„ ์ฐจํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ Œ๋”๋ง ์ž‘์—…์„ ๋‹ค์‹œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • Transition ์—…๋ฐ์ดํŠธ๋Š” ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  • ๋งŒ์•ฝ ์ง„ํ–‰ ์ค‘์ธ transition์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ, React์—์„œ๋Š” ํ•จ๊ป˜ ์ผ๊ด„ ์ฒ˜๋ฆฌ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ–ฅํ›„ ๋ฆด๋ฆฌ์ฆˆ์—์„œ ์ œ๊ฑฐ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์ œํ•œ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.


์‚ฌ์šฉ๋ฒ•

state ์—…๋ฐ์ดํŠธ๋ฅผ non-blocking transition์œผ๋กœ ํ‘œ์‹œ

startTransition์œผ๋กœ ๋ž˜ํ•‘ํ•จ์œผ๋กœ์จ state ์—…๋ฐ์ดํŠธ๋ฅผ transition์œผ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import { startTransition } from 'react';

function TabContainer() {
const [tab, setTab] = useState('about');

function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
// ...
}

Transition์„ ์‚ฌ์šฉํ•˜๋ฉด ๋Š๋ฆฐ ์žฅ์น˜์—์„œ๋„ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ์—…๋ฐ์ดํŠธ์˜ ๋ฐ˜์‘์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Transition์„ ์‚ฌ์šฉํ•˜๋ฉด UI๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋„์ค‘์—๋„ ๋ฐ˜์‘์„ฑ์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž๊ฐ€ ํƒญ์„ ํด๋ฆญ ํ–ˆ๋‹ค๊ฐ€ ๋งˆ์Œ์ด ๋ฐ”๋€Œ์–ด ๋‹ค๋ฅธ ํƒญ์„ ํด๋ฆญํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ฆฌ๋ Œ๋”๋ง์ด ์™„๋ฃŒ๋  ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด ํ•„์š” ์—†์ด ๋‹ค๋ฅธ ํƒญ์„ ํด๋ฆญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค!

startTransition์€ useTransition๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ, transition์ด ์ง„ํ–‰ ์ค‘์ธ์ง€ ์ถ”์ ํ•˜๋Š” isPending ํ”Œ๋ž˜๊ทธ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. useTransition์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์„ ๋•Œ startTransition์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, startTransition์€ ๋ฐ์ดํ„ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ์™€ ๊ฐ™์ด ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

Transition์— ๋Œ€ํ•œ ํ•™์Šต ๋ฐ ์˜ˆ์ œ๋Š” useTransition ํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•˜์„ธ์š”.