입력받아 변경하기

miin·2021년 12월 17일
0

Skill Collection [Function]

목록 보기
12/46

결과

내용

  • 정해져 있는 제목을 버튼을 누르면 입력받은 내용으로 변경하기

코드

import {useState} from 'react';

export default function App() {
  const [title, setTitle] = useState('hello')
  const [name, setName] = useState('')
  
  const onChange = e => {
    setName(e.target.value)
  }

  const clickHandler = () => {
    setTitle(name)
  }

  return (
    <div>
      <button onClick={clickHandler}>Change Title</button>
      <input type="text" onChange={onChange}/>
      <h1>{title}</h1>
    </div>
  );
}

0개의 댓글