HalfWebDev
HalfWebDev

Reputation: 7638

Typescript conversion error for select event and default value

TSlint throws an error on defaulValue - Type 'string' is not assignable to type 'ChangeEvent<HTMLInputElement> | undefined - for following code

const App = () => {
  const [ month, setMonth] = useState("last1")
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
      console.log(e);
      setMonth(e.target.value);
  }
  ...

and then inside return

  <Select defaultValue={month} style={{ width: 120 }} onChange={handleChange}>

Upvotes: 2

Views: 826

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281626

The Select component that you use would most probably be passing the value to the onChange handler instead of the event and hence you get that warning

You should instead define it like

const App = () => {
  const [ month, setMonth] = useState("last1")
  const handleChange = (value: string) => {
      setMonth(value);
  }
  ...

Upvotes: 1

Related Questions