### 1. 이벤트 생성
React에서 이벤트를 처리하는 방법
Example
<button onClick={}>
----
</button>
→ on 뒤에 대문자로 시작한다!
그 외의 차이점
Example
<header>
<h1><a href="/" onClick={function(e){
e.preventDefault();
this.props.onChangePage(); //props로 전달된 함수 호출
}.bind(this)}>{this.props.title}</a></h1>
{this.props.sub}
</header>
: 사용자가 폼을 제출하면 새로운 페이지로 이동하는 기본 HTML폼 동작을 수행한다.
React에서는 JavaScript함수로 폼의 제출을 처리하고 사용자가 입력한 데이터에 접근하도록 하는 것이 편리하다.(제어 컴포넌트 방식)
Create버튼을 누르면 새로운 내용을 추가할 수 있는 폼을 만들어보자.
CreateContent.js (일부 생략)
render(){
return(
<article>
<h2>Create</h2>
<form acrion="/create_process" method="post"
onSubmit={function(e){
e.preventDefault();
this.props.onSubmit(
//title과 desc의 값을 이벤트에 넘겨줌
e.target.title.value,
e.target.desc.value
);
}.bind(this)}
>
// 입력하는 칸 구현
<p>
<input
type="text"
name="title"
placeholder="title"
></input>
</p>
<p>
<textarea name="desc" placeholder="description"></textarea>
</p>
<p>
<input type="submit"></input>
placeholder : 미리보기랄까? 텍스트를 입력하는 칸에 무엇을 입력해야하는지 연하게 쓰여 있음.
App.js 일부
<form acrion="/create_process" method="post"
onSubmit={function(e){
e.preventDefault();
this.props.onSubmit(
e.target.title.value,
e.target.desc.value
);
}.bind(this)}
>
<p><input type="text" name="title"
placeholder="title"></input></p>
<p>
<textarea name="desc" placeholder="description"></textarea>
</p>
<p>
<input type="submit"></input>
</p>
</form>
단, 배열의 경우에만 사용 가능.
객체에서는 Object.assign()을 사용하면 된다.
code 관련은 프로그램 참고!!