리액트 배열에 데이터 추가하기
다음은 동물 종류와 동물 울음소리를 지정한 배열입니다. 이를 state에 animals 라는 이름으로 저장하겠습니다. //animals state, 동물과 울음소리가 들어있음 const [animals, setAnimals] = useState([ { id : 1, animalType : '고양이', sound : '야옹' }, { id : 2, animalType : '강아지', sound : '멍멍' }, { id : 3, animalType : '닭', sound : '꼬꼬댁' } ]); input을 이용하여 데이터를 추가하기 위해 다음과 같은 메서드를 선언하였습니다. //animals state에 새롭개 생성된 animal 객체 추가 const onCreate=()=>{ const animal =..
2022. 5. 13.