Ignore fist parameter of function if unused with a simple _ (underscore)
Tuesday, November 10, 2020
Ignore fist parameter of function if unused with a simple _ (underscore)
If you ever find yourself in a situation where you need to implement function receiving multiple parameters, but you don't really need all of them, know that there is a way how to get rid of the unused parameter warning/error. It can be as simple as using an underscore as prefix or even whole name of parameter _
.
There is even a way how to ignore first n params, but that might be too much, you decide.
Code Examples
callback fn with first parameter ignored
const callbackFn = (_, item) => item.id
callback fn with first parameter ignored and second one destructed to what we actually need
const callbackFn = (_, {id}) => id
callback fn with unused first parameter
const callbackFn = (index, item) => item.id
callback fn with unreasonably used parameter to avoid the warning/error
const callbackFn = (index, item) => {
console.log(index);
return item.id;
}
Have a question or comment?