Files
study/modern-react/my-react/src/chap04/StateFormUC.js

33 lines
905 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>
);
}