9

In My React Component I have Radio input

<input type="radio" onChange={changeData}>

and

function changeData(e: any) {}

How can I specify event type I don't want to use any. enter image description here

2
  • can you provide screenshot of error? Commented Jun 26, 2020 at 9:47
  • I have added screenshot Commented Jun 26, 2020 at 10:06

5 Answers 5

16

Check this out

changeData = (e: React.ChangeEvent<HTMLInputElement>)=> {
    .....
}
Sign up to request clarification or add additional context in comments.

1 Comment

for radio inputs I get this error Type '(event: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type '(e: RadioChangeEvent) => void'. Types of parameters 'event' and 'e' are incompatible. Type 'RadioChangeEvent' is missing the following properties from type 'ChangeEvent<HTMLInputElement>': currentTarget, bubbles, cancelable, defaultPrevented, and 7 more.
0
changeData = (e: React.MouseEvent<HTMLInputElement>): void => { // code }

3 Comments

for radio inputs I get this error Type '(event: React.MouseEvent<HTMLInputElement>) => void' is not assignable to type '(e: RadioChangeEvent) => void'. Types of parameters 'event' and 'e' are incompatible. Type 'RadioChangeEvent' is missing the following properties from type 'MouseEvent<HTMLInputElement>': currentTarget, bubbles, cancelable, defaultPrevented, and 7 more.
what about now? still same?
can you search for 'RadioChangeEvent' and replace it with some valid type?
0

One might want to write it this way:

const changeData: React.ChangeEventHandler | undefined = 
  (event: React.ChangeEvent<HTMLTextAreaElement>) => {}

and then

<input type="radio" onChange={changeData}>

Comments

0

Just do this:

changeData = (event: Event) => {
      const { value } = event.target as unknown as { value: boolean, };
      setState(value);
};
  
<input type='radio' onChange={changeData} />

And the squeegee lines will go away.

Comments

0

You can specify the event type as React.ChangeEvent:

const changeData = (e: React.ChangeEvent<HTMLInputElement>) => {
  // your implementation here
};

<input type="radio" onChange={changeData} />

The React.ChangeEvent type is a generic type that represents a change event in a DOM element. The HTMLInputElement type represents an HTML element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.