src/App.tsx
export default function App() {
const handleCapture = () => {
console.log('Parent');
};
const handleBubble = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.stopPropagation();
console.log('Child');
};
return (
<div
onClick={handleCapture}
style={{ padding: '50px', backgroundColor: '#f0f0f0' }}
>Parent
<button onClick={handleBubble} style={{ marginTop: '20px' }}
>Click Me</button>
</div>
);
}
애플리케이션을 실행하고 버튼을 클릭하면 콘솔에 다음과 같은 출력만 나타납니다. 이는 이벤트가 발생할 때 이벤트 객체의 stopPropagation() 메서드가 호출되어 이벤트가 부모로 전달되지 않았기 때문입니다.