Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

javascript - 如何在这里摆脱代码重复(棉绒错误)(How to get rid of code repetition here (lint errors))

Here's my code, and I would like to get rid of this because it's causing me lint errors, but I don't have idea how to refactor this to get this somehow cleaner etc..

(这是我的代码,我想摆脱它,因为它会导致我出现棉绒错误,但是我不知道如何重构它,以使其更清洁。)

formatter: value => {
      switch (value) {
        case PRODUCT_STATUS.Sold:
          return (
            <span className="product">
              PRODUCT
            </span>
          );
        case PRODUCT_STATUS.OnStock:
          return (
            <span className="product success">
              ON STOCK
            </span>
          );
        case PRODUCT_STATUS.Expired:
          return (
            <span className="product error">
              EXPIRED
            </span>
          );
        default:
          return 'STATUS DEFAULT';
      }
}

Thanks guys

(多谢你们)

Cheers

(干杯)

  ask by Roxy'Pro translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you create a function to generate the template and call that in each case, then I think the linter will be happy:

(如果您创建一个函数来生成模板并在每种情况下调用该模板,那么我认为lint将会很高兴:)

function template(cssClasses, text) {
   return `
     <span class="${cssClasses}">
       ${text}
     </span>
  `;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...