Home πŸ“Έ 2022-07-19
Post
Cancel

πŸ“Έ 2022-07-19

πŸ”₯ 였늘 어디에 도전해보셨고, 무엇을 λ°°μš°μ…¨λ‚˜μš”?

🌊 ν•™μŠ΅ν•˜μ‹œλ©΄μ„œ κΆκΈˆν•˜μ‹  λΆ€λΆ„μ΄λ‚˜, μ–΄λ €μ› λ˜ 점은 λ­˜κΉŒμš”?

  • React.createElement으둜 ν•˜λ‚˜μ˜ νƒœκ·Έλ§Œ λ°˜ν™˜ν•΄μ•Ό ν•œλ‹€κ³  ν•˜λŠ”λ° μ΄μœ κ°€ ꢁ금
  • μ˜ˆμ „λΆ€ν„° λ“£κΈ°λŠ” 많이 λ“€μ—ˆλ˜ λ””λ°”μš΄μ‹±(Debouncing)κ³Ό μ“°λ‘œν‹€λ§(Throttling) 더 μžμ„Ένžˆ μ•Œμ•„λ³΄κΈ°

🌟 내일은 μ–΄λ–»κ²Œ 해보고 μ‹ΆμœΌμ„Έμš”?

  • React - useReducer μ•Œμ•„λ³΄κΈ°

Reactμ—μ„œ JSX μš”μ†Œ λ°˜ν™˜ν•˜κΈ°

ν•˜λ‚˜μ˜ μ»΄ν¬λ„ŒνŠΈλŠ” ν–” 개의 μš”μ†Œλ§Œμ„ λ°˜ν™˜ν•΄μ•Ό ν•œλ‹€. λ”°λΌμ„œ JSX 문법을 μ‚¬μš©ν•  λ•Œμ—λŠ” μ•„λž˜μ™€ 같이 ν•˜λ‚˜μ˜ νƒœκ·Έλ§Œ λ°˜ν™˜ν•΄μ•Ό ν•œλ‹€.

1
2
3
4
5
6
const Content = () => {
  return (
      <h2>title</h2>
      <p>description</p>
  );
};

Content μ»΄ν¬λ„ŒνŠΈλŠ” titleκ³Ό description을 λ³΄μ—¬μ£ΌλŠ” νƒœκ·Έλ‘œ κ΅¬μ„±λ˜μ–΄μžˆλ‹€.

ν•˜μ§€λ§Œ λ°˜ν™˜ μ‹œμ—λŠ” ν•˜λ‚˜μ˜ νƒœκ·Έλ§Œ λ°˜ν™˜ν•΄μ•Ό ν•˜λ―€λ‘œ μœ„μ™€ 같이 μ»΄νŒŒμΌμ„ ν•˜κ²Œ 되면 였λ₯˜κ°€ λ‚œλ‹€. JSX 문법을 μ‚¬μš©ν•˜μ—¬ μ»΄ν¬λ„ŒνŠΈ λ‚΄μš©μ„ λ°˜ν™˜ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ‹€μŒκ³Ό 같은 방법이 μžˆλ‹€.

  1. μ΅œμƒμœ„μ— divνƒœκ·Έλ‘œ 감싸기

    1
    2
    3
    4
    5
    6
    7
    8
    
    const content = () => {
      return (
        <div>
          <h2>title</h2>
          <p>description</p>
        </div>
      );
    };
    

    κ°€μž₯ κ°„λ‹¨ν•˜κ³  많이 μ‚¬μš©ν•˜λŠ” λ°©λ²•μ΄μ§€λ§Œ 이와 같이 κ°μ‹ΈλŠ” μš©λ„μ˜ divλ₯Ό μ‚¬μš©ν•œ μ—¬λŸ¬ μ»΄ν¬λ„ŒνŠΈκ°€ 겹쳐지면 <div> soup(div 쀑첩)κ°€ λ°œμƒν•œλ‹€.

    divκ°€ 많이 μ€‘μ²©λ μˆ˜λ‘ 속도가 λŠλ €μ§€κ³  μŠ€νƒ€μΌμ„ μ μš©ν•  λ•Œμ—λ„ λ²ˆκ±°λ‘œμ›€μ΄ μžˆμ„ 수 μžˆλ‹€.

  2. JSX μš”μ†Œλ“€μ„ 배열에 λ‹΄μ•„ λ°˜ν™˜

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    const content = () => {
    return (
        [
        <h2>title</h2>
        , <p>description</p>
        ]
        </div>
    );
    };
    

    λ°°μ—΄λ‘œ λ°˜ν™˜ν•˜κ²Œ 되면 λͺ©λ‘ μœ ν˜•μ˜ μ»΄ν¬λ„ŒνŠΈμ™€ 같이 각 μš”μ†Œλ§ˆλ‹€ key 값을 λΆ€μ—¬ν•΄μ•Ό ν•œλ‹€.

  3. UI μš”μ†Œ μ—†λŠ” λˆˆμ†μž„ μ»΄ν¬λ„ŒνŠΈλ‘œ 감싸기

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    const Wrapper = (props) => {
      return props.children;
    };
    
    const content = () => {
      return (
        <Wrapper>
          <h2>title</h2>
          <p>description</p>
        </Wrapper>
      );
    };
    

    μ—¬κΈ°μ„œ Wrapper μ»΄ν¬λ„ŒνŠΈλŠ” JSX λ¬Έλ²•μœΌλ‘œ UI μš”μ†Œλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  κ·Έμ € νŒŒλΌλ―Έν„°λ‘œ 전달받은 props의 ν•˜μœ„ λ‚΄μš©λ§Œ λ°˜ν™˜ν•œλ‹€.

    content μ»΄ν¬λ„ŒνŠΈμ˜ λ‚΄μš©μ„ Wrapper μ»΄ν¬λ„ŒνŠΈλ‘œ 감싸주면 λΆˆν•„μš”ν•œ div νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ λ˜μ–΄ μ½”λ“œλ₯Ό κ΄€λ¦¬ν•˜κ³  μŠ€νƒ€μΌμ„ μ μš©ν•˜κΈ°μ— 훨씬 μˆ˜μ›”ν•˜λ‹€.

  4. <React.fragment>(λ˜λŠ” <></>) μ‚¬μš©ν•˜κΈ° 3번의 λˆˆμ†μž„ μ»΄ν¬λ„ŒνŠΈμ˜ κΈ°λŠ₯을 λ¦¬μ•‘νŠΈμ—μ„œ μ œκ³΅ν•΄μ£ΌλŠ”λ° 이것이 λ°”λ‘œ <React.fragment>이닀.

This post is licensed under CC BY 4.0 by the author.

πŸ“– μ‹€μš©μ£Όμ˜ ν”„λ‘œκ·Έλž˜λ¨Έ

πŸ“Έ 2022-07-21