name
속성
name
속성을 이용해서 <input>
을 구분 가능.
<input name='id' />
console.log(e.target.name);
type
type='text'
, type='password'
등 속성 사용 가능.
<input name='id' type='text' />
placeholder
<input name='id' type='text' placeholder='아이디' />
사용 예시
import React, { useState } from 'react';
function InputSample() {
const [inputs, setInputs] = useState({
id: '',
password: '',
});
const { id, password } = inputs;
function valChange(e) {
const { name, value } = e.target;
console.log(e.target.name);
setInputs({
...inputs,
[name]: value,
});
}
function onReset() {
setInputs({
id: '',
password: '',
});
}
return (
<>
<input name='id' type='text' placeholder='아이디' onChange={valChange} value={id} />
<input name='password' type='password' placeholder='비밀번호' onChange={valChange} value={password} />
<button onClick={onReset}>초기화</button>
<div>
<b>값: </b>
{id} ({password})
</div>
</>
);
}
export default InputSample;