[modern-react] 리액트 스터디 파일 추가
This commit is contained in:
33
modern-react/my-react/src/chap04/StateFormUC.js
Normal file
33
modern-react/my-react/src/chap04/StateFormUC.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useRef } from 'react';
|
||||
|
||||
export default function StateFormUC() {
|
||||
// 리액트 요소에 대한 참조 준비
|
||||
const name = useRef(null);
|
||||
const age = useRef(null);
|
||||
|
||||
// 요소(참조)를 통해 입력값 준비하기
|
||||
const show = () => {
|
||||
console.log(`안녕하세요, ${name.current.value}(${age.current.value}세) 님!`);
|
||||
};
|
||||
|
||||
// 폼 그리기
|
||||
return (
|
||||
<form>
|
||||
{/* 준비된 레퍼런스를 각 요소에 연결 */}
|
||||
<div>
|
||||
<label htmlFor="name">이름: </label>
|
||||
<input id="name" name="name" type="text"
|
||||
ref={name} defaultValue="홍길동" />
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="age">나이: </label>
|
||||
<input id="age" name="age" type="number"
|
||||
ref={age} defaultValue="18" />
|
||||
</div>
|
||||
<div>
|
||||
<button type="button" onClick={show}>
|
||||
보내기</button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user