Hi I am using a ternary operator inside a function. But eslint throws error on this. Pls help me to fix this.
const Test = (showBtn, bubbleId, latitude, longitude, zoom, drillLevel) => {
setShowBtn(showBtn);
drillLevel === 'area' ? getCitiesData(bubbleId) : getStatesData();
setViewport({
...viewport,
latitude,
longitude,
zoom,
});
};
This rule aims to eliminate unused expressions which have no effect on the state of the program. This rule does not apply to function calls or constructor calls with the new operator, because they could have side effects on the state of the program.
If your current code works, it would probably be more appropriate to avoid the conditional operator entirely and use if
/else
instead:
if (drillLevel === 'area') getCitiesData(bubbleId)
else getStatesData();
This way, you don't actually have any unused expressions, which is the right way to fix this sort of linter warning.
You can simply add this allowTernary
comment above the ternary operator expression, to disable eslint error for that line:
/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
drillLevel === 'area' ? getCitiesData(bubbleId) : getStatesData();
May be you forget to assign get* result to some variable, waht about viewport
?
const data = drillLevel === 'area' ?
getCitiesData(bubbleId) :
getStatesData();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With