DOM의 이벤트 처리 방식과 유사
HTML에서의 이벤트 처리 방식
<button onclick="handleEvent()">Event</button>
React의 이벤트 처리 방식
<button onClick={handleEvent}>Event</button>
function Parent() {
return (
<div className="parent">
<h1>I'm the parent</h1>
<Child text={"I'm the eldest child"} />
<Child />
</div>
);
}
function Child(props) {
return (
<div className="child">
<p>{props.text}</p>
</div>
);
}
props를 전달하는 또 다른 방법으로 여는 태그와 닫는 태그의 사이에 value를 넣어 전달하는 방법이 있다. 이 경우 props.children을 이용하면 해당 value에 접근하여 사용할 수 있다.
function Parent() {
return (
<div className="parent">
<h1>I'm the parent</h1>
<Child>I'm the eldest child</Child>
</div>
);
};
function Child(props) {
return (
<div className="child">
<p>{props.children}</p>
</div>
);
};