๐Ÿ“– TIL - React ๋งˆ์Šคํ„ฐํ•˜๊ธฐ: ํด๋กœ์ €๋ถ€ํ„ฐ ํด๋ž˜์Šค๋„ค์ž„๊นŒ์ง€

์Š˜ยท2025๋…„ 1์›” 22์ผ

๐Ÿ“– TIL

๋ชฉ๋ก ๋ณด๊ธฐ
33/90

๐Ÿ“Œ ํ•™์Šต ์š”์•ฝ

์˜ค๋Š˜์€ React์˜ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ๋“ค์„ ๊นŠ์ด ์žˆ๊ฒŒ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ํด๋กœ์ €์˜ ํ™œ์šฉ, ์ปดํฌ๋„ŒํŠธ ์ค‘์ฒฉ, ํผ ์ œ์ถœ ์ตœ์ ํ™”, ๊ทธ๋ฆฌ๊ณ  ํด๋ž˜์Šค๋„ค์ž„ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ง‘์ค‘์ ์œผ๋กœ ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿค” ์ฃผ์š” ๊ฐœ๋… ๋ฐ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

1. ํด๋กœ์ €์™€ ํ•จ์ˆ˜ ๋ฐ˜ํ™˜

์ž˜๋ชป๋œ ๋ฐฉ์‹

function debounce(func, delay) {
  let timer;
  clearTimeout(timer);  // ์ฆ‰์‹œ ์‹คํ–‰๋˜์–ด ๋ฒ„๋ฆผ
  timer = setTimeout(() => func(), delay);
}

์ด ๋ฐฉ์‹์˜ ๋ฌธ์ œ์ :

  • ํ•จ์ˆ˜๊ฐ€ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๊ณ  ๋๋‚จ
  • timer ๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜ ์ข…๋ฃŒ์™€ ํ•จ๊ป˜ ์‚ฌ๋ผ์ง
  • debounce ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์Œ

์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ์‹ (ํด๋กœ์ € ํ™œ์šฉ)

function debounce(func, delay) {
  let timer;  // ํด๋กœ์ €๋กœ ์œ ์ง€๋˜๋Š” ๋ณ€์ˆ˜
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => func(...args), delay);
  };
}

์žฅ์ :

  • timer ๋ณ€์ˆ˜๊ฐ€ ํด๋กœ์ €๋กœ ์œ ์ง€๋จ
  • ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • ์‹ค์ œ debounce ํšจ๊ณผ ๊ตฌํ˜„ ๊ฐ€๋Šฅ

2. props.children์„ ํ†ตํ•œ ์ปดํฌ๋„ŒํŠธ ์ค‘์ฒฉ

๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

const ParentComponent = ({ children }) => {
  return (
    <div>
      <h1>Parent Title</h1>
      <div>{children}</div>
    </div>
  );
};

// ์‚ฌ์šฉ ์˜ˆ์‹œ
<ParentComponent>
  <ChildComponent />
</ParentComponent>

๊ณ ๊ธ‰ ํ™œ์šฉ๋ฒ•

const ParentComponent = ({ children }) => {
  // ์ž์‹ ์š”์†Œ ์กฐ์ž‘
  React.Children.forEach(children, child => {
    console.log(child.type);
  });

  // ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง
  if (!Array.isArray(children)) {
    return <div>Single child: {children}</div>;
  }

  return (
    <div>
      {children.map((child, index) => (
        <div key={index}>{child}</div>
      ))}
    </div>
  );
};

3. Form ์ œ์ถœ ์ด๋ฒคํŠธ ์ตœ์ ํ™”

Before (๊ฐœ๋ณ„ onClick ์ด๋ฒคํŠธ)

<form className={styles.formGrid}>
  <button 
    type="button"
    onClick={(e) => addNationHandler(e)} 
    className={styles.formButton}
  >
    ๊ตญ๊ฐ€ ์ถ”๊ฐ€
  </button>
  <button 
    type="button"
    onClick={(e) => modifyNationHandler(e)} 
    className={styles.formButton}
  >
    ์—…๋ฐ์ดํŠธ
  </button>
</form>

After (ํ†ตํ•ฉ onSubmit ์ด๋ฒคํŠธ)

const handleSubmit = (e) => {
  e.preventDefault();
  const buttonName = e.nativeEvent.submitter.name;
  
  if (buttonName === 'add') {
    addNationHandler();
  } else if (buttonName === 'modify') {
    modifyNationHandler();
  }
};

<form className={styles.formGrid} onSubmit={handleSubmit}>
  <button type="submit" name="add" className={styles.formButton}>
    ๊ตญ๊ฐ€ ์ถ”๊ฐ€
  </button>
  <button type="submit" name="modify" className={styles.formButton}>
    ์—…๋ฐ์ดํŠธ
  </button>
</form>

4. ํด๋ž˜์Šค๋„ค์ž„ ๊ด€๋ฆฌ ์ „๋žต

1) Props ์ง์ ‘ ์ „๋‹ฌ ๋ฐฉ์‹

const TableWrapper = ({ children, wrapperClassName, headerClassName }) => (
  <table className={wrapperClassName}>
    <thead className={headerClassName}>
      {children}
    </thead>
  </table>
);

2) classnames ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ™œ์šฉ

import classNames from 'classnames';

const TableWrapper = ({ 
  children, 
  wrapperClassName, 
  headerClassName, 
  isActive 
}) => {
  const tableClass = classNames(wrapperClassName, {
    'active-class': isActive,
    'table-wrapper': true
  });

  return (
    <table className={tableClass}>
      <thead className={headerClassName}>
        {children}
      </thead>
    </table>
  );
};

๐Ÿ’ก ์‹ค๋ฌด ์ ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

  1. ํด๋กœ์ € ์‚ฌ์šฉ ์‹œ

    • ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์ฃผ์˜
    • ๋ถˆํ•„์š”ํ•œ ํด๋กœ์ € ์ƒ์„ฑ ํ”ผํ•˜๊ธฐ
    • ํด๋กœ์ €์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๊ณ ๋ ค
  2. children ํ™œ์šฉ ์‹œ

    • ํƒ€์ž… ์ฒดํฌ ํ•„์ˆ˜
    • key ์†์„ฑ ์ ์ ˆํžˆ ์‚ฌ์šฉ
    • ๋ถˆํ•„์š”ํ•œ ์ค‘์ฒฉ ํ”ผํ•˜๊ธฐ
  3. ํผ ์ œ์ถœ ์ฒ˜๋ฆฌ ์‹œ

    • preventDefault() ํ•„์ˆ˜
    • ๋ฒ„ํŠผ์˜ name ์†์„ฑ ํ™œ์šฉ
    • ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง ๊ณ ๋ ค
  4. ํด๋ž˜์Šค๋„ค์ž„ ๊ด€๋ฆฌ ์‹œ

    • ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ์— ๋”ฐ๋ฅธ ์ „๋žต ์„ ํƒ
    • CSS ๋ชจ๋“ˆ๊ณผ์˜ ์กฐํ•ฉ ๊ณ ๋ ค
    • ์„ฑ๋Šฅ ์˜ํ–ฅ ๊ฒ€ํ† 

๐ŸŽฏ ๊ฐœ์„  ๋ฐ ๋ฐœ์ „ ๋ฐฉํ–ฅ

  • ํด๋กœ์ € ํŒจํ„ด์˜ ๋‹ค์–‘ํ•œ ํ™œ์šฉ ๋ฐฉ์•ˆ ์—ฐ๊ตฌ
  • ์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉ์„ฑ ํ–ฅ์ƒ ๋ฐฉ๋ฒ• ๊ณ ๋ฏผ
  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง ์ตœ์ ํ™” ์ „๋žต ์ˆ˜๋ฆฝ
  • CSS-in-JS ๋„์ž… ๊ฒ€ํ† 
profile
์ฃผ๋‹ˆ์–ด ํ”„๋ก ํŠธ์—”๋“œ ์„ฑ์žฅ๊ธฐ ๊ธฐ๋ก๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€