简体   繁体   English

预期在箭头函数错误的末尾返回一个值

[英]Expected to return a value at the end of arrow function error

I have this error Expected to return a value at the end of arrow function consistent-return. 我有此错误,期望在箭头函数一致返回的末尾返回一个值。 Im not sure how I can prevent this. 我不确定如何防止这种情况。

Im trying to stop the Swiper carousel when the screen-size is below 1060px 我试图在屏幕尺寸低于1060px时停止Swiper轮播

    import Swiper from 'swiper';

    export default function () {
      let articlesGalleryCarousel;

      const doSomething = () => {
        const enableSwiper = () => {
          articlesGalleryCarousel = new Swiper('.js-swiper-container', {
            loop: true,
            slidesPerView: 'auto',
            centeredSlides: true,
            a11y: true,
            keyboardControl: true,
            grabCursor: true,
            pagination: '.swiper-pagination',
            paginationClickable: true,
            navigation: {
              nextEl: '.carousel-button--prev',
              prevEl: '.carousel-button--next',
            },
          });
        };

        const breakpoint = window.matchMedia('(max-width:1060px)');

        const breakpointChecker = () => {
          if (breakpoint.matches === true) {
            if (articlesGalleryCarousel !== undefined) articlesGalleryCarousel.destroy(true, true);
          } else if (breakpoint.matches === false) {
            return enableSwiper();
          }
        };

        breakpoint.addListener(breakpointChecker);

        breakpointChecker();
      };
      return doSomething;
    }

This is an ESLint consistent-return warning. 这是ESLint一致返回警告。

"This rule requires return statements to either always or never specify values" “此规则要求return语句必须始终或从不指定值”

Your function "breakpointChecker" did not returns something in case of the first IF statement. 在第一个IF语句的情况下,函数“ breakpointChecker”未返回任何内容。

ESLint is telling you that an arrow function should always or never return a value. ESLint 告诉您箭头功能应该始终或永远不返回值。

You have one branch that returns a value ( return enableSwiper(); ) and one that doesn't (if breakpoint.matches is true). 您有一个分支返回一个值( return enableSwiper(); ),而没有分支(如果breakpoint.matches为true)。

So -- do you want that function to always return a value or to never return a value? 那么-您是否希望该函数始终返回值或从不返回值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM