如何修复此 Eslint 使用展开运算符而不是 '.apply()'

问题描述 投票:0回答:3

我学习了反应,但不明白如何解决这个问题。来自这个来源
我在规则详细信息中查找此规则:

使用扩展运算符而不是“.apply()”。

但是已经尝试过,但它从未有效地重新编写此代码以遵守此规则:

renderPages() {
    const { pdf, containerWidth, zoom } = this.state;
    const { disableVisibilityCheck } = this.props;
    if (!pdf) return null;
    const pages = Array.apply(null, { length: pdf.numPages });
    return pages.map((v, i) => (
        <PDFPage
            index={i + 1}
            key={`pdfPage_${uniqueId()}`}
            pdf={pdf}
            containerWidth={containerWidth}
            zoom={zoom * INCREASE_PERCENTAGE}
            disableVisibilityCheck={disableVisibilityCheck}
        />
    ));
}
arrays reactjs apply eslint
3个回答
5
投票

消息建议使用这种表达方式:

const pages = { ...{ length: pdf.numPages }};

例如,以下代码可以编译:

const somePdf = {
  numPages: 10,
  pageSize: 'A4',
}

const pages = { ...{ length: somePdf.numPages }};

console.log(JSON.stringify(pages))

// returns "{ length: 10}"

1
投票

我认为该规则并不那么重要,因此如果您愿意,可以通过将此行添加到

eslintrc.js
文件中的规则中来禁用该规则:

'rules': {
  ...
  'prefer-spread': ['off']
}

0
投票

“规则”:{ “@typescript-eslint/no-this-alias”:[ “错误”, { “允许解构”:true, “allowedNames”:[“自我”] } ], “@typescript-eslint/no-empty-function”:[ “错误”, { “允许”: [ “功能”, “吸气剂”, “设定者”, "箭头函数", “方法”, “构造函数” ] } ], “首选传播”:[“关闭”] }

© www.soinside.com 2019 - 2024. All rights reserved.