<star />

λ¦¬μ•‘νŠΈμ˜ ν•©μ„±κ³Ό 상속 λ³Έλ¬Έ

React πŸ“

λ¦¬μ•‘νŠΈμ˜ ν•©μ„±κ³Ό 상속

별깅 2022. 12. 3. 19:40
728x90

 

λ¦¬μ•‘νŠΈμ˜ 합성은 μ—¬λŸ¬ 개의 μ»΄ν¬λ„ŒνŠΈλ₯Ό ν•©μ³μ„œ μƒˆλ‘œμš΄ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ§Œλ“œλŠ” 것을 μ˜λ―Έν•¨.

합성이라고 ν•΄μ„œ λ¬΄μž‘μ • κ·Έλƒ₯ μ»΄ν¬λ„ŒνŠΈλ“€μ„ λΆ™μ΄λŠ” 것이 μ•„λ‹ˆλΌ, μ—¬λŸ¬ 개의 μ»΄ν¬λ„ŒνŠΈλ₯Ό μ–΄λ–»κ²Œ μ‘°ν•©ν•  것인가에 λŒ€ν•œ 고민이 ν•„μš”ν•¨.

 

1) Containment

 

ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ₯Ό ν¬ν•¨ν•˜λŠ” ν˜•νƒœμ˜ ν•©μ„± 방법.

λ¦¬μ•‘νŠΈ μ»΄ν¬λ„ŒνŠΈμ˜ props 에 기본적으둜 λ“€μ–΄ μžˆλŠ” children 속성을 μ‚¬μš©ν•˜λ©΄ 됨.

 

function FancyBorder (props) {
    return (
        <div className={'FancyBorder FancyBorder-' + props.color}>
            {props.children}
        </div>
    )
}

 

μœ„ μ½”λ“œμ—μ„œ FancyBorder λΌλŠ” μ»΄ν¬λ„ŒνŠΈμ— props.children 을 μ‚¬μš©ν•˜λ©΄ ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈμ˜ ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈκ°€ λͺ¨λ‘ children 으둜 λ“€μ–΄μ˜€κ²Œ 됨.

children μ΄λΌλŠ” prop 은 κ°œλ°œμžκ°€ 직접 λ„£λŠ” 게 μ•„λ‹ˆλΌ λ¦¬μ•‘νŠΈμ—μ„œ 기본적으둜 μ œκ³΅ν•΄ μ£ΌλŠ” κ²ƒμž„.

 

λ¦¬μ•‘νŠΈμ˜ createElement() ν•¨μˆ˜λ₯Ό μ‚΄νŽ΄λ³΄λ©΄...

 

React.createElement (
    type,
    [props],
    [...children]
)

 

여기에 μ„Έ λ²ˆμ§Έμ— λ“€μ–΄κ°€λŠ” νŒŒλΌλ―Έν„°κ°€ λ°”λ‘œ children μž„.

children 이 λ°°μ—΄λ‘œ λ˜μ–΄ μžˆλŠ” μ΄μœ λŠ” μ—¬λŸ¬ 개의 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ₯Ό κ°€μ§ˆ 수 있기 λ•Œλ¬Έμž„.

결과적으둜 μœ„ μ½”λ“œμ˜ FancyBorder μ»΄ν¬λ„ŒνŠΈλŠ” μžμ‹ μ˜ ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ₯Ό λͺ¨λ‘ ν¬ν•¨ν•˜μ—¬ 예쁜 ν…Œλ‘λ¦¬λ‘œ κ°μ‹Έμ£ΌλŠ” μ»΄ν¬λ„ŒνŠΈκ°€ 됨.

 

function Welcome (props) {
    return(
        <FancyBorder color="pink">
            <h1 className="Welcome-title">
                μ–΄μ„œμ˜€μ„Έμš”!
            </h1>
            <p className="Welcome-msg">
                별깅이 λΈ”λ‘œκ·Έμ— μ˜€μ‹  것을 ν™˜μ˜ν•©λ‹ˆλ‹€!
            </p>
        </FancyBorder>
    );
}

 

μœ„ μ½”λ“œλ₯Ό 보면 Welcome μ»΄ν¬λ„ŒνŠΈ μ•ˆμ— FancyBorder μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜κ³  있음.

FancyBodrder μ»΄ν¬λ„ŒνŠΈλ‘œ 감싸진 λΆ€λΆ„μ—λŠ” <h1> νƒœκ·Έμ™€ <p> νƒœκ·Έ μ΄λ ‡κ²Œ 두 개의 νƒœκ·Έκ°€ λ“€μ–΄κ°€ μžˆμŠ΅λ‹ˆλ‹€.

이 두 개의 νƒœκ·ΈλŠ” λͺ¨λ‘ FancyBorder μ»΄ν¬λ„ŒνŠΈμ— children μ΄λΌλŠ” μ΄λ¦„μ˜ props둜 전달됨.

결과적으둜 뢄홍색 ν…Œλ‘λ¦¬λ‘œ λͺ¨λ‘ κ°μ‹Έμ§€λŠ” κ²°κ³Όκ°€ λ‚˜μ˜¬ κ²ƒμž„.

 

λ¦¬μ•‘νŠΈμ—μ„œλŠ” props.children 을 톡해 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ₯Ό ν•˜λ‚˜λ‘œ λͺ¨μ•„μ„œ μ œκ³΅ν•΄ 쀌.

 

μ—¬λŸ¬ 개의 children 집합이 ν•„μš”ν•œ κ²½μš°λŠ” μ–΄λ–»κ²Œ ν• κΉŒ? 

γ„΄ λ³„λ„λ‘œ props λ₯Ό μ •μ˜ν•΄μ„œ 각각 μ›ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό λ„£μ–΄ μ£Όλ©΄ 됨.

 

function SplitPane (props) {
    return(
        <div className="SplitPane">
            <div className="SplitPane-top">
                {props.top}
            </div>
            <div className="SplitPane-btm">
                {props.btm}
            </div>
        </div>
    );
}

function App (props) {
    return (
        <SplitPane
            top={
                <Store />
            }
            btm={
                <board />
            }
        />
    );
}

 

μœ„μ˜ μ½”λ“œλŠ” 화면을 μƒν•˜λ‹¨μœΌλ‘œ λΆ„ν• ν•΄μ„œ 보여 μ£ΌλŠ” SplitPane μ΄λΌλŠ” μ»΄ν¬λ„ŒνŠΈκ°€ 있음.

그리고 μ•„λž˜μͺ½μ— λ‚˜μ™€ μžˆλŠ” App μ»΄ν¬λ„ŒνŠΈμ—μ„œλŠ” 이 SplitPane μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜κ³  μžˆλŠ”λ°, topκ³Ό btm μ΄λΌλŠ” 두 개의 props λ₯Ό μ •μ˜ν•˜μ—¬ κ·Έ μ•ˆμ— 각각 λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ„£μ–΄ μ£Όκ³  있음.

SplitSpane μ—μ„œλŠ” top, btm 을 props 둜 λ°›κ²Œ 되고, 각각 ν™”λ©΄μ˜ 상단과 ν•˜λ‹¨μ— λΆ„λ¦¬ν•΄μ„œ λ Œλ”λ§ ν•˜κ²Œ 됨.

이처럼 μ—¬λŸ¬ 개의 children 집합이 ν•„μš”ν•œ κ²½μš°μ—λŠ” λ³„λ„μ˜ props λ₯Ό μ •μ˜ν•΄μ„œ μ‚¬μš©ν•˜λ©΄ 됨.

 

props.children μ΄λ‚˜ 직접 μ •μ˜ν•œ propsλ₯Ό μ΄μš©ν•˜μ—¬ ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ₯Ό ν¬ν•¨ν•˜λŠ” ν˜•νƒœλ‘œ ν•©μ„±ν•˜λŠ” 방법을

Containment 라고 함.

 

 

2) Specialization

 

λ²”μš©μ μΈ κ°œλ…μ„ κ΅¬λ³„λ˜κ²Œ κ΅¬μ²΄ν™”ν•˜λŠ” 것을 말함.

 

function Dialog (props) {
    return (
        <FancyBorder color="pink">
            <h1 className="Diglog-title">
                {props.title}
            </h1>
            <p className="Dialog-msg">
                {props.msg}
            </p>
        </FancyBorder>
    );
}

function WelcomeDialog (props) {
    return (
        <Diglog
            title="μ•ˆλ†ν•˜μ„Έμš”"
            msg="별깅이 λΈ”λ‘œκ·Έμ— μ˜€μ‹  것을 ν™˜μ˜ν•©λ‹ˆλ‹€~"
        />
    );
}

 

Dialog λΌλŠ” λ²”μš©μ μΈ μ»΄ν¬λ„ŒνŠΈμ™€ 이것을 μ‚¬μš©ν•˜λŠ” WelcomeDialog μ»΄ν¬λ„ŒνŠΈκ°€ λ‚˜μ˜΄.

Dialog μ»΄ν¬λ„ŒνŠΈλŠ” title, msg λΌλŠ” 두 κ°€μ§€ props λ₯Ό κ°€μ§€κ³  있음.

이 title κ³Ό msg λ₯Ό μ–΄λ–»κ²Œ μ‚¬μš©ν•˜λŠλƒμ— λ”°λΌμ„œ κ²½κ³  λ‹€μ΄μ–Όλ‘œκ·Έκ°€ 될 μˆ˜λ„ 있고, 인사말 λ‹€μ΄μ–Όλ‘œκ·Έκ°€ 될 μˆ˜λ„ μžˆλŠ” κ±°μž„.

 

κ·ΈλŸ¬λ‹ˆκΉŒ, λ²”μš©μ μœΌλ‘œ μ“Έ 수 μžˆλŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό λ§Œλ“€μ–΄ 놓고 이λ₯Ό νŠΉμˆ˜ν™” μ‹œμΌœμ„œ μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” ν•©μ„± λ°©μ‹μž„.

 

 

 

3) Containment 와 Specialization 을 같이 μ‚¬μš©ν•˜κΈ°

 

λ‘˜μ„ 같이 μ“°λ €λ©΄ 일단...

Containment λ₯Ό μœ„ν•΄ props.children 을 μ‚¬μš©ν•˜κ³ ,

Specialization 을 μœ„ν•΄ 직접 μ •μ˜ν•œ props λ₯Ό μ‚¬μš©ν•˜λ©΄ 됨.

 

function Dialog (props) {
    return (
        <FancyBorder color="pink">
            <h1>
                {props.title}
            </h1>
            <p>
                {props.msg}
            </p>
            {props.children}
        </FancyBorder>
    );
}

function SignUpDialog (props) {
    const [nickname, setNickname] = useState("");
    const handleChange = (event) => {
        setNickname(event.target.value);
    }
    const handleSignUp = () => {
        alret(`ν™˜μ˜ν•΄μš”, ${nickname}λ‹˜~!`);
    }

    return (
        <Diglog
            title="별깅이 λΈ”λ‘œκ·Έ"
            msg="λ‹‰λ„€μž„μ„ μž…λ ₯ν•˜μ‹œμ˜€"
        >
            <input
                value={nickname}
                onChange={handleChange}
            />
            <button
                onClick={handleSignUp}
            >
            μž…λ ₯ν•˜κΈ°
            </button>
        </Diglog>
    );
}

 

μœ„μ˜ μ½”λ“œμ—μ„œ Diglog μ»΄ν¬λ„ŒνŠΈλŠ” Containment  λ₯Ό μœ„ν•΄ 끝 뢀뢄에 props.children 을 μΆ”κ°€ν–ˆμŒ.

이λ₯Ό 톡해 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈκ°€ λ‹€μ΄μ–Όλ‘œκ·Έ ν•˜λ‹¨μ— λ Œλ”λ§ 됨.

 

Diglog λ₯Ό μ‚¬μš©ν•˜λŠ” SignUpDialog μ»΄ν¬λ„ŒνŠΈλŠ” Specialization 을 μœ„ν•΄ props인 title κ³Ό msg 에 값을 λ„£μ–΄ μ£Όκ³  있고,

μ‚¬μš©μžλ‘œλΆ€ν„° λ‹‰λ„€μž„μ„ μž…λ ₯λ°›κΈ° μœ„ν•΄ <input> κ³Ό <button> νƒœκ·Έκ°€ λ“€μ–΄ 있음.

이 두 개의 νƒœκ·ΈλŠ” λͺ¨λ‘ props.children 으둜 μ „λ‹¬λ˜μ–΄ λ‹€μ΄μ–Όλ‘œκ·Έμ— ν‘œμ‹œλ¨!

 

μ΄λŸ¬ν•œ ν˜•νƒœλ‘œ Containment 와 Specialization 을 λ™μ‹œμ— μ‚¬μš©ν•  수 있음.

각 방법을 λ”°λ‘œ μ‚¬μš©ν•˜κ±°λ‚˜, λ˜λŠ” λ™μ‹œμ— ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ λ‹€μ–‘ν•˜κ³  λ³΅μž‘ν•œ μ»΄ν¬λ„ŒνŠΈλ₯Ό 효율적으둜 κ°œλ°œν•  수 있음.

 

 

 

4) 상속

 

ν•©μ„±κ³Ό λŒ€λΉ„λ˜λŠ” κ°œλ…μœΌλ‘œ 상속(Inheritance) 이 있음.

컴퓨터 ν”„λ‘œκ·Έλž˜λ°μ—μ„œμ˜ 상속은 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ λ‚˜μ˜¨ κ°œλ…μž„.

λΆ€λͺ¨ 클래슀λ₯Ό μƒμ†λ°›μ•„μ„œ, μƒˆλ‘œμš΄ μžμ‹ 클래슀λ₯Ό λ§Œλ“ λ‹€λŠ” κ°œλ…μœΌλ‘œ μžμ‹ ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ν΄λž˜μŠ€κ°€ κ°€μ§„ λ³€μˆ˜λ‚˜ ν•¨μˆ˜ λ“±μ˜ 속성을 λͺ¨λ‘ κ°–κ²Œ 됨.

 

λ¦¬μ•‘νŠΈμ—μ„œλŠ” μƒμ†μ΄λΌλŠ” 방법을 μ‚¬μš©ν•˜λŠ” κ²ƒλ³΄λ‹€λŠ” μ•žμ—μ„œ 배운 합성을 μ‚¬μš©ν•΄μ„œ κ°œλ°œν•˜λŠ” 것이 더 쒋은 λ°©λ²•μž„.

λ”°λΌμ„œ, λ³΅μž‘ν•œ μ»΄ν¬λ„ŒνŠΈλ₯Ό μͺΌκ°œμ„œ μ—¬λŸ¬ 개의 μ»΄ν¬λ„ŒνŠΈλ‘œ λ§Œλ“€κ³ , λ§Œλ“  μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ‘°ν•©ν•˜μ—¬ μƒˆλ‘œμš΄ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ§Œλ“œμ…ˆ!

 

 

728x90

'React πŸ“' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Provider value μ—μ„œ μ£Όμ˜ν•΄μ•Ό ν•  사항  (0) 2022.12.05
μ»¨ν…μŠ€νŠΈ  (0) 2022.12.04
State λŒμ–΄μ˜¬λ¦¬κΈ°  (0) 2022.12.01
λ¦¬μ•‘νŠΈμ˜ 폼  (0) 2022.11.30
λ¦¬μŠ€νŠΈμ™€ ν‚€  (0) 2022.11.29
Comments