Case로 보는 좋은 컴포넌트

좋은 컴포넌트 분리

Case 1) 추상화와 추출을 구분하기

// before
<section>
	<h1>회의실 예약</h1>
	<section>
		<label>
			<span>선호 층</span>
			<select>
				<option>1</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
			</select>
		</label>
		<label>
			<span>필요 장비</span>
			<div>
				<button>스피커</button>
				<button>마우스</button>
				<button>마이크</button>
			</div>
		</label>
	</section>
</section>
// after
<section>
	<h1>회의실 예약</h1>
	<FilterPanel/>
</section>

before -> after로 개선했다고 가정했을 때 어떤 점이 좋지 않을까?

<section>
	<h1>회의실 예약</h1>
	<section>
		<label>
			<span>선호 층</span>
			<NumberSelect />
		</label>
		<label>
			<span>필요 장비</span>
			<ToggleGroup />
		</label>
	</section>
</section>

관련 블로그 글:추출이 아닌, 추상화

Case 2) 좋은 추상화는 맥락을 가지지 않는다

// bad
fucntion FloorSelect () {
	return (
		<select>
			<option>1</option>
			<option>2</option>
			<option>3</option>
			<option>4</option>
		</select>
	)
}
// good
export function NumberSelect({ value, onChange, options, ariaLabel, label, placeholder = '전체' }: Props) {

return (
	<Select
		value={value ?? ''}
		onChange={e => {
			const val = e.target.value;
			onChange(val === '' ? null : Number(val));
		}}
		aria-label={ariaLabel}
    >								
		<option value="">{placeholder}</option>
		{options.map(opt => (
			<option key={opt} value={opt}>
				{opt}
				{label}
			</option>
		))}
	</Select>
	);
}

Case 3) 컴포넌트와 Props의 연결성

// bad
<MyReservationList setMessage={setMessage} />

출처
토스 frontend fundamentals 모의고사 2회차 해설