ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

web/react

startTransition

dev_been94 2024. 12. 16. 11:50
728x90
๋ฐ˜์‘ํ˜•

startTransition

 

๐ŸŽฏ startTransition์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?

startTransition์€ React์—์„œ ๊ธด๊ธ‰ํ•˜์ง€ ์•Š์€ ํ™”๋ฉด ์—…๋ฐ์ดํŠธ๋ฅผ "์šฐ์„ ์ˆœ์œ„๋ฅผ ๋‚ฎ์ถฐ์„œ" ์‹คํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ์ด์—์š”.
๋ณต์žกํ•œ ์ž‘์—…์œผ๋กœ ์ธํ•ด ํ™”๋ฉด์ด ๋ฒ„๋ฒ…๊ฑฐ๋ฆฌ๋Š” ๊ฑธ ๋ง‰๊ณ , ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ™”๋ฉด์„ ์ „ํ™˜ํ•ด์ค˜์š”.

 

๐Ÿ• ๋น„์œ ๋กœ ์ดํ•ดํ•˜๊ธฐ: ํ–„๋ฒ„๊ฑฐ ๊ฐ€๊ฒŒ ์ด์•ผ๊ธฐ

์—ฌ๋Ÿฌ๋ถ„์ด ํ–„๋ฒ„๊ฑฐ ๊ฐ€๊ฒŒ๋ฅผ ์šด์˜ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”. ๐Ÿ”

  1. ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ
    • ์†๋‹˜์ด ๋™์‹œ์— ์—ฌ๋Ÿฌ ์ฃผ๋ฌธ์„ ํ•ด์š”.
    • **ํ–„๋ฒ„๊ฑฐ 10๊ฐœ๋ฅผ ๋งŒ๋“œ๋Š” ์ผ(๋ฌด๊ฑฐ์šด ์ž‘์—…)**๊ณผ ํ•จ๊ป˜ **์ƒˆ๋กœ์šด ์†๋‹˜์ด ์ฃผ๋ฌธํ•˜๋Š” ๊ฑธ ๋ฐ›๋Š” ์ผ(๊ธด๊ธ‰ํ•œ ์ž‘์—…)**์ด ๊ฒน์น˜๋ฉด ๊ฐ€๊ฒŒ๋Š” ์ •์‹ ์ด ์—†๊ฒ ์ฃ ?
    • ์ด๋Ÿด ๋• ์†๋‹˜ ์ฃผ๋ฌธ๋„ ์ œ๋Œ€๋กœ ๋ชป ๋ฐ›๊ณ  ๊ฐ€๊ฒŒ๊ฐ€ ๋ฒ„๋ฒ…์ผ ๊ฑฐ์˜ˆ์š”.
  2. startTransition์„ ์‚ฌ์šฉํ•˜๋ฉด?
    • "ํ–„๋ฒ„๊ฑฐ 10๊ฐœ ๋งŒ๋“œ๋Š” ๊ฑด ์ž ๊น ๋’ค๋กœ ๋ฏธ๋ฃจ๊ณ , ๋จผ์ € ์ƒˆ๋กœ์šด ์†๋‹˜ ์ฃผ๋ฌธ๋ถ€ํ„ฐ ๋ฐ›์•„์•ผ์ง€!"
    • ์ฆ‰, ๊ธด๊ธ‰ํ•œ ์ผ(์ž…๋ ฅ, ํด๋ฆญ ๋“ฑ)์„ ๋จผ์ € ์ฒ˜๋ฆฌํ•˜๊ณ , ๋ฌด๊ฑฐ์šด ์ž‘์—…์€ ๋‚˜์ค‘์— ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์†๋‹˜์„ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ํ•˜์ง€ ์•Š๊ณ  ๊ฐ€๊ฒŒ๊ฐ€ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋Œ์•„๊ฐ€๊ฒ ์ฃ ?

React์—์„œ startTransition๋„ ๊ฐ™์€ ์—ญํ• ์„ ํ•ด์š”!
ํ™”๋ฉด์„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ „ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”ํ•œ ์ž‘์—…(๊ธด๊ธ‰)์„ ๋จผ์ € ์ฒ˜๋ฆฌํ•˜๊ณ , ๋œ ์ค‘์š”ํ•œ ์ž‘์—…์€ ๋‚˜์ค‘์— ์‹คํ–‰ํ•ด์ค๋‹ˆ๋‹ค.

 

๐Ÿ› ๏ธ startTransition ์‚ฌ์šฉ๋ฒ•

startTransition์€ React์˜ useTransition ํ›…๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
์ด์ œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณผ๊ฒŒ์š”!

 

์˜ˆ์ œ: ๊ธด๊ธ‰ํ•œ ์—…๋ฐ์ดํŠธ์™€ ๋œ ์ค‘์š”ํ•œ ์—…๋ฐ์ดํŠธ

1. ๊ธฐ๋ณธ ์ฝ”๋“œ (startTransition ์—†์ด)

import React, { useState } from 'react';

export default function App() {
  const [input, setInput] = useState('');
  const [items, setItems] = useState([]);

  const handleChange = (e) => {
    const value = e.target.value;
    setInput(value); // ์ž…๋ ฅ ๊ฐ’ ์—…๋ฐ์ดํŠธ
    const newItems = Array(10000).fill(value); // ๋ฌด๊ฑฐ์šด ์ž‘์—…: 10,000๊ฐœ์˜ ํ•ญ๋ชฉ ์ƒ์„ฑ
    setItems(newItems); // ํ™”๋ฉด์— ์ƒˆ๋กœ์šด ๋ชฉ๋ก ํ‘œ์‹œ
  };

  return (
    <div>
      <h1>StartTransition ์˜ˆ์ œ</h1>
      <input value={input} onChange={handleChange} placeholder="์ž…๋ ฅํ•ด๋ณด์„ธ์š”" />
      <ul>
        {items.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </div>
  );
}

์„ค๋ช…:

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ์ฐฝ์— ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด handleChange๊ฐ€ ์‹คํ–‰๋ผ์š”.
  • ๋ฌธ์ œ์ :
    ์ž…๋ ฅ์ฐฝ์— ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•  ๋•Œ๋งˆ๋‹ค 10,000๊ฐœ์˜ ํ•ญ๋ชฉ์ด ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ™”๋ฉด์ด ๋ฒ„๋ฒ…๊ฑฐ๋ฆด ์ˆ˜ ์žˆ์–ด์š”.
    ์‚ฌ์šฉ์ž๊ฐ€ ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ธด๊ธ‰ํ•œ ์ž‘์—…๋„ ๋Šฆ์–ด์ง€๊ฒ ์ฃ ?

 

2. startTransition์„ ์‚ฌ์šฉํ•ด์„œ ์ตœ์ ํ™”ํ•˜๊ธฐ

startTransition์„ ์‚ฌ์šฉํ•ด ์ž…๋ ฅ๊ฐ’(๊ธด๊ธ‰ ์ž‘์—…)์€ ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธํ•˜๊ณ , ํ•ญ๋ชฉ ์ƒ์„ฑ(๋œ ๊ธด๊ธ‰ํ•œ ์ž‘์—…)์€ ๋‚˜์ค‘์— ์‹คํ–‰๋˜๋„๋ก ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š”.

import React, { useState, startTransition } from 'react';

export default function App() {
  const [input, setInput] = useState('');
  const [items, setItems] = useState([]);

  const handleChange = (e) => {
    const value = e.target.value;
    setInput(value); // ์ž…๋ ฅ ๊ฐ’์€ ๋ฐ”๋กœ ์—…๋ฐ์ดํŠธ (๊ธด๊ธ‰ํ•œ ์ž‘์—…)

    // startTransition์œผ๋กœ ๋ฌด๊ฑฐ์šด ์ž‘์—… ์ฒ˜๋ฆฌ (๋œ ์ค‘์š”ํ•œ ์ž‘์—…)
    startTransition(() => {
      const newItems = Array(10000).fill(value);
      setItems(newItems);
    });
  };

  return (
    <div>
      <h1>StartTransition ์˜ˆ์ œ</h1>
      <input value={input} onChange={handleChange} placeholder="์ž…๋ ฅํ•ด๋ณด์„ธ์š”" />
      <ul>
        {items.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </div>
  );
}

๐Ÿ” ์ฝ”๋“œ ์„ค๋ช…

  1. ๊ธด๊ธ‰ํ•œ ์ž‘์—…
    • setInput(value)๋Š” ์ฆ‰์‹œ ์‹คํ–‰๋ผ์„œ ์ž…๋ ฅ์ฐฝ์— ์ž…๋ ฅ์ด ๋ฐ”๋กœ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค.
  2. ๋œ ์ค‘์š”ํ•œ ์ž‘์—…
    • startTransition ์•ˆ์— setItems๋ฅผ ๋„ฃ์–ด์„œ 10,000๊ฐœ์˜ ํ•ญ๋ชฉ ์ƒ์„ฑ๊ณผ ๊ฐ™์€ ๋ฌด๊ฑฐ์šด ์ž‘์—…์€ ๋‚˜์ค‘์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  3. ๊ฒฐ๊ณผ
    • ์ž…๋ ฅ์ฐฝ์— ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋™์ž‘์ด ๋ฒ„๋ฒ…์ด์ง€ ์•Š๊ณ  ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ฐ˜์‘ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฌด๊ฑฐ์šด ์ž‘์—…์€ ์ž…๋ ฅ์ด ๋๋‚œ ํ›„์— ์‹คํ–‰๋˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์ด ์ข‹์•„์ ธ์š”.

 

๐Ÿš€ startTransition์ด ํ•„์š”ํ•œ ์ด์œ 

  1. UI ๋ฒ„๋ฒ…์ž„ ๋ฐฉ์ง€
    • ๋ฌด๊ฑฐ์šด ์ž‘์—… ๋•Œ๋ฌธ์— ์ž…๋ ฅ์ด๋‚˜ ํด๋ฆญ ๊ฐ™์€ ๊ธด๊ธ‰ํ•œ ์ž‘์—…์ด ๋Š๋ ค์ง€๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ์ค๋‹ˆ๋‹ค.
  2. ์‚ฌ์šฉ์ž ๊ฒฝํ—˜ ํ–ฅ์ƒ
    • ํ™”๋ฉด์ด ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ „ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์ด ํ›จ์”ฌ ์ข‹์•„์ง‘๋‹ˆ๋‹ค.
  3. ์šฐ์„ ์ˆœ์œ„ ์ œ์–ด
    • React๊ฐ€ ๊ธด๊ธ‰ํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ๋จผ์ € ์ฒ˜๋ฆฌํ•˜๊ณ  ๋œ ์ค‘์š”ํ•œ ์ž‘์—…์€ ๋‚˜์ค‘์— ์‹คํ–‰ํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

 

โš ๏ธ ์ฃผ์˜ํ•  ์ 

  1. React 18 ์ด์ƒ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • startTransition์€ React 18 ๋ฒ„์ „๋ถ€ํ„ฐ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.
  2. ๋ฌด์กฐ๊ฑด ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ์•„๋‹ˆ์—์š”
    • ๋ชจ๋“  ์ž‘์—…์— startTransition์„ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†๊ณ , ๋ฌด๊ฑฐ์šด ์ž‘์—…์ด UI๋ฅผ ๋ฐฉํ•ดํ•  ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ผ์š”.

 

๐ŸŒŸ ํ•œ ์ค„ ์š”์•ฝ

**startTransition**์€ ๊ธด๊ธ‰ํ•œ ์ž‘์—…(์˜ˆ: ์ž…๋ ฅ, ํด๋ฆญ)์ด ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ฒ˜๋ฆฌ๋˜๋„๋ก ํ•˜๊ณ , ๋ฌด๊ฑฐ์šด ์ž‘์—…์€ ๋‚˜์ค‘์— ์‹คํ–‰ํ•˜๊ฒŒ ๋„์™€์ฃผ๋Š” ๊ธฐ๋Šฅ์ด์—์š”.

 

 

๐ŸŽ‰ ๋งˆ๋ฌด๋ฆฌ

React์˜ startTransition์€ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฉ‹์ง„ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.
ํ™”๋ฉด์ด ๋ฒ„๋ฒ…๊ฑฐ๋ฆฌ๊ฑฐ๋‚˜ ๋Š๋ ค์งˆ ๋•Œ ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด UI๋ฅผ ๊น”๋”ํ•˜๊ณ  ๋งค๋„๋Ÿฝ๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์–ด์š”. ๐Ÿ˜Š

728x90
๋ฐ˜์‘ํ˜•

'web > react' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

experimental_taintObjectReference  (0) 2024.12.16
use  (0) 2024.12.16
memo  (0) 2024.12.16
lazy  (0) 2024.12.16
forwardRef  (0) 2024.12.16
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ
๋ฐ˜์‘ํ˜•
250x250