'event' is deprecated- what to use instead for React change handlers
Asked Answered
B

1

8

I am currently building a React app with Material UI. I'm trying to build a Select component with a dropdown, and as I'm writing the handleChange function, I am getting a warning that my function parameter event is deprecated.

I've seen this before when editing other applications with functions that depend on an event variable. Looking online, I've seen recommendations to use .bind or .addEventListenerinstead, but I don't totally understand how these work. It also seemingly makes the code way uglier...

I guess I'm just looking for some advice as to how I would implement these alternatives in the case of a MUI Select component. This is what I want to write:

const handleChange= (event: any) => {
  setState(event.target.value);
};

So that I could easily use it in the render function:

   <Select
      native
      value={binary}             \\ (state variable)
      onChange={handleChange}    \\ (nice and simple function ref)
      inputProps={{
        name: 'binary',
        id: 'binary-select',
      }}
   >

If event is deprecated, am I risking a loss of functionality in the future by using it? If I should use these alternative solutions I've seen online instead, how would I apply them in this case?

Thanks!

Bwana answered 23/8, 2021 at 19:3 Comment(3)
What is the actual issue you are facing? From the docs it looks like even the demo (material-ui.com/components/selects/#native-select) is using onChange and passing the event. Why do you say its deprecated ?Diplomacy
What exactly is telling you event is deprecated? I don't see anything about it being deprecated in the docs (even for v5)Gland
Hmmm @EmmanuelPonnudurai I guess VSCode was mistaking my event paramter as the GLOBAL event handler, which is deprecated. As you've noted, using event as a parameter is perfectly okay!Bwana
B
8

I guess VSCode was mistaking my event paramter as the GLOBAL event handler, which is deprecated. As noted by others' comments, using event as a parameter is perfectly okay!

'Problem' solved.

Bwana answered 23/8, 2021 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.