Uncontrolled Component

DOM이 폼 요소의 값을 직접 관리하는 방식입니다.


예시

function Form() {
    const inputRef = useRef();

    const handleSubmit = () => {
        console.log(inputRef.current.value);  // DOM에서 직접 읽음
    };

    return (
        <div>
            <input ref={inputRef} defaultValue="초기값" />
            <button onClick={handleSubmit}>제출</button>
        </div>
    );
}

동작 흐름

1. 사용자가 'a' 입력
2. DOM의 input 요소가 값을 저장
3. React는 모름 (리렌더링 없음)
4. 필요할 때 ref로 DOM에서 값을 읽어옴

Controlled vs Uncontrolled

ControlledUncontrolled
값 저장React stateDOM
값 설정valuedefaultValue
값 접근state 변수ref.current.value
리렌더링입력마다 발생없음

언제 쓰는가

// 파일 입력 (Uncontrolled만 가능)
<input type="file" ref={fileRef} />

// 간단한 폼 (굳이 state 필요 없을 때)
<input ref={nameRef} defaultValue="홍길동" />
상황선택
실시간 검증 필요Controlled
입력값 가공 필요Controlled
파일 입력Uncontrolled
간단한 폼Uncontrolled

핵심 차이

Controlled: React가 주인 (state → DOM)
Uncontrolled: DOM이 주인 (DOM → ref로 읽음)

CSLOG에서 보기