[ERR] Type 'ChangeEvent<HTMLInputElement>' is not assignable to type 'SetStateAction<string>'.

최경락 (K_ROCK_)·2022년 5월 26일
0

ERR

Type 'Dispatch<SetStateAction<string>>' is not assignable to type 'ChangeEventHandler<HTMLInputElement>'.
  Types of parameters 'value' and 'event' are incompatible.
Type 'ChangeEvent<HTMLInputElement>' is not assignable to type 'SetStateAction<string>'.

WHEN

  • input 이 변경될 때 해당 내용을 상태로 저장하고자 했을 때 생긴 에러.
const [inputValue, setInputValue] = useState<string>('');

<input type="text" onChange={setInputValue} />

WHY

  • 에러 메시지 상에서 무언가 변경되는 이벤트를 뜻하는 타입에 상태를 변경시키는 함수를 뜻하는 타입을 할당 할 수 없다는 내용을 표시했다.
  • 완벽하게 이해가 되진 않지만, 에러 메시지로 보아 서로 다른 타입이기 때문에 충돌한 것으로 보인다.
  • 이제보니 문법상으로도 잘못사용하긴 했다 ㅎㅎ....

SOLVE

const [inputValue, setInputValue] = useState<string>('');

<input type="text" onChange={(e) => setInputValue(e.target.value)} />
  • 위와 같이 함수를 직접 할당하는 것이 아니라, 함수 표현식으로 작성하여 해결했다.

  • 코드는 차근차근 보자....

0개의 댓글