함수형 컴포넌트에서 useState로 상태관리하는 배열의 항목 추가하기
import React, { useRef, useState } from "react";
function App() {
const [log, setLog] = useState([]);
const [inputValue, setInputValue] = useState("");
const handleChange = (e) => {
setInputValue(e.target.value);
};
const handleClick = (e) => {
setLog([...log, inputValue]);
console.log(log);
setInputValue("");
};
return (
<div>
<input
type="text"
onChange={handleChange}
name="inputValue"
value={inputValue}
></input>
<button type="button" onClick={handleClick}>
작성
</button>
</div>
);
}
export default App;