이벤트(event)란 웹 브라우저가 알려주는 HTML 요소에 대한 사건의 발생을 의미한다.
유저의 행동에 의해 발생할 수도 있으며 개발자가 의도한 로직에 의해 발생할 수도 있다.
const App = () => {
const handleClick = () => {
alert("클릭했습니다.");
}
return (
<div>
<button onClick={handleClick}>클릭하세요</button>
</div>
);
};
✔ 주요 DOM 이벤트
onClick: Element를 클릭했을 때
onChange: Element의 내용이 변경되었을 때 (input의 텍스트를 변경, 파일 선택 등)
onKeyDown, onKeyUp, onKeyPress: 키보드 입력이 일어났을 때
onDoubleClick: Element를 더블 클릭했을 때
onFocus: Element에 Focus되었을 때
onBlur: Element가 Focus를 잃었을 때
onSubmit: Form Element에서 Submit 했을 때
const App = () => {
return (
<div>
<button onClick={() => { alert('클릭했습니다.') }
}>클릭하세요</button>
</div>
)
}