0

Specifically, I am using react-semantic-ui's Select Form, which takes an onChange function. So I have defined my function as such:

const handleInputChange = function handleInputChange(event, data){// does something with data}

(don't mind the ugly syntax, that's airbnb style guide)

I am doing something with data, but I am not using the event parameter at all. What would be the best practice in this case, since it is generally a bad practice to declare a variable without using it?

Samson
  • 1,336
  • 2
  • 13
  • 28

2 Answers2

3

You must declare a parameter for every argument before the one you want to use.

The idiomatic approach to marking a parameter as unused is to prefix its name with an underscore.

function handleInputChange(_event, data){

Some linters will recognise this and not emit a "unused variable" warning.

For example ESLint has a argsIgnorePattern option (but it also has a args option that lets you specify after-used).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It's also very common to name a parameter simply underscore (`_`) if it's not used. However, it only works for *one* parameter, e.g., `function foo(_, _, myParam)` is not valid. It also might clashes with the Underscore/Lodash libraries (or others) which define an object called `_`. – VLAZ May 19 '22 at 08:26
1

Actually by using Js es6 you could code without unused argument:)

function handleInputChange(...[, data]) {
  console.log(data);
}

handleInputChange('event', 123);

And bellow is a similar question you could check out:) Standard conventions for indicating a function argument is unused in JavaScript

JuneC
  • 59
  • 7