首页 > 解决方案 > 如何修复这个 Eslint 使用扩展运算符而不是 '.apply()'

问题描述

我学习反应并且不明白如何解决这个问题。从这个来源
,我查看了这个规则的规则详细信息

使用扩展运算符而不是 '.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}
        />
    ));
}

标签: arraysreactjsapplyeslint

解决方案


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

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

例如,以下代码编译:

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

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

console.log(JSON.stringify(pages))

// returns "{ length: 10}"

推荐阅读