如何修复此Eslint,请使用传播运算符代替“ .apply”

问题描述

我学会了反应,但不知道如何解决。来自这个source
我在Rule Details中查找此规则:

使用传播运算符代替“ .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}
        />
    ));
}

解决方法

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

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

例如,以下代码编译:

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

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

console.log(JSON.stringify(pages))

// returns "{ length: 10}"