Project : 근의 공식(Muscle Formula)/Error Handling

2022/03/23 TS 리액트에서 event 타입 지정

괴발새발자 2022. 3. 20. 21:08
function Test () {

  const [value, setValue] = useState('');
  const handleInputValue = (e :React.ChangeEvent<HTMLInputElement>) => {
    setNumber(e.target.value);
  }

  return (
  //...
  <input type="text" placeholder="값을 입력해주세요" onChange={handleInputValue}/>
  //...
  )
}

해결책 : 이벤트(e)의 타입을 제대로 지정해준다. VS Code 참조해서 쓰면 알기 쉽다.

참고자료 : https://stackoverflow.com/questions/64649055/type-changeeventhtmlinputelement-is-not-assignable-to-type-changeeventhtml#

 

Type 'ChangeEvent<HTMLInputElement>' is not assignable to type 'ChangeEvent<HTMLSelectElement>' in React js?

I am passing an onChange event as a props to a component But I am getting datatype error. Parent Component - const onChangeStatus=(selectType:string , e:React.ChangeEvent<HTMLSelectElement>) ...

stackoverflow.com