Tord Larsen
Tord Larsen

Reputation: 2836

How to fix this Eslint Use the spread operator instead of '.apply()'

I learn react and dont understand how to fix this. From this source
I look in the Rule Details for this rule:

Use the spread operator instead of '.apply()'.

But have tried but it's never working to reformate this code to comply with this rule:

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}
        />
    ));
}

Upvotes: 5

Views: 14668

Answers (3)

Mehdi Benhamouche
Mehdi Benhamouche

Reputation: 99

when using eslintrc.json, add "prefer-spread": ["off"] in rules.

Here are some rules that i use in one of my projects:

"rules": {
"@typescript-eslint/no-this-alias": [
  "error",
  {
    "allowDestructuring": true,
    "allowedNames": ["self"]
  }
],
"@typescript-eslint/no-empty-function": [
  "error",
  {
    "allow": [
      "functions",
      "getters",
      "setters",
      "arrowFunctions",
      "methods",
      "constructors"
    ]
  }
],
"prefer-spread": ["off"]}

Upvotes: 0

Boommeister
Boommeister

Reputation: 2127

I think that rule is not that important, so if you want you can simply disable the rule by adding this line to your rules in the eslintrc.js file:

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

Upvotes: 1

Juangui Jord&#225;n
Juangui Jord&#225;n

Reputation: 6597

The message suggest using this kind of expression:

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

For instance, the following code compiles:

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

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

console.log(JSON.stringify(pages))

// returns "{ length: 10}"

Upvotes: 5

Related Questions