最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

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,
    });
  };
Share Improve this question asked Apr 18, 2020 at 10:29 SDKSDK 1,5183 gold badges28 silver badges55 bronze badges 3
  • eslint also shows line and column, can you update question with more details? – Lukas Liesis Commented Apr 18, 2020 at 10:31
  • it throws error on the drillLevel line – SDK Commented Apr 18, 2020 at 10:32
  • Then check this one stackoverflow.com/a/61287706/1737158 – Lukas Liesis Commented Apr 18, 2020 at 10:35
Add a comment  | 

4 Answers 4

Reset to default 6

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

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 }]*/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论