Input Event는 입력 시 발생하는 이벤트이다. 다음과 같은 종류가 존재한다.
class MainComponent extends React.Component {
state = {
text : "",
};
setText = (e)=>{
this.setState({
text:e.target.value
});
};
render(){
const {text} = this.state;
return (
<>
<input type="text" onChange={this.setText}/>
<hr/>
<span>입력 글자 수 : {text.length}</span>
</>
);
}
}
const app = ReactDOM.createRoot(document.querySelector("#app"));
app.render(
<MainComponent/>
);
class MainComponent extends React.Component {
state = {
memberId:"",
memberNick:"",
memberPhone:"",
};
setValue = (e)=>{
this.setState({
[e.target.name]:e.target.value
});
};
render(){
const {memberId, memberNick, memberPhone} = this.state;
return (
<>
아이디 : <input type="text" name="memberId" onInput={this.setValue}/><br/>
닉네임 : <input type="text" name="memberNick" onInput={this.setValue}/><br/>
전화번호 : <input type="text" name="memberPhone" onInput={this.setValue}/><br/>
<hr/>
<div>입력된 아이디 : {memberId}</div>
<div>입력된 닉네임 : {memberNick}</div>
<div>입력된 전화번호 : {memberPhone}</div>
</>
);
}
}
const app = ReactDOM.createRoot(document.querySelector("#app"));
app.render(
<MainComponent/>
);