SDK
SDK

Reputation: 1508

Expected an assignment or function call and instead saw an expression.eslintno-unused-expressions

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,
    });
  };

Upvotes: 8

Views: 30885

Answers (4)

CertainPerformance
CertainPerformance

Reputation: 370689

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.

Upvotes: 6

Lukas Liesis
Lukas Liesis

Reputation: 26403

https://eslint.org/docs/rules/no-unused-expressions

add option to your eslint to allowTernary = true

You can add it as a comment above the line or in eslint config where you have your rules defined

/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/

enter image description here

Upvotes: 1

Kyr
Kyr

Reputation: 1025

May be you forget to assign get* result to some variable, waht about viewport?

const data = drillLevel === 'area' ?
  getCitiesData(bubbleId) :
  getStatesData();

Upvotes: 1

palaѕн
palaѕн

Reputation: 73896

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();

Upvotes: 2

Related Questions