32

How can I resolve the eslint error: "prop spreading is forbidden" in a custom route component?

This error occurs on lines 3 and 6 below:

const PrivateRoute = ({component: Component, ...rest}) => (
  <Route
    {...rest}
    render={(props) => (
        localStorage.getItem('user') ?
          <Component {...props} /> :
          <Redirect to={{pathname: '/login', state: {from: props.location}}} />
  )}
  />
);
Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
Almaz Kaliev
  • 468
  • 1
  • 4
  • 10

4 Answers4

42

ES lint discourages the use of prop spreading so that no unwanted/unintended props are being passed to the component. More details here: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md

To disable it for the particular file, you can put: /* eslint-disable react/jsx-props-no-spreading */ at the top line in your component file. For disabling it for all files, try this: Disable in EsLint "react / jsx-props-no-spreading" error in Reactjs

Edited comments as per answer below

michaelward82
  • 4,706
  • 26
  • 40
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • 7
    thank u! i added a rule "react/jsx-props-no-spreading": ["error", {"custom": "ignore"}] and it solved the problem – Almaz Kaliev Nov 06 '19 at 09:09
29

For ESLint is important which type of comment do you use, the legit one is:

/* eslint-disable react/jsx-props-no-spreading */
Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73
  • 2
    This ... after banging my head against the wall for way too long. Comment type does matter apparently. – FMaz008 Dec 13 '20 at 00:14
0

If you are using vscode and have eslint plugin installed then simply hover over the error it will open the dialog as shown in image -> quick fix -> disable for this line.

Doing this will add proper lint comment automatically.

enter image description here

Foolish
  • 3,952
  • 33
  • 46
0

You can use this one just to comment out the below line:-

/* eslint-disable-next-line react/jsx-props-no-spreading */
maverick
  • 82
  • 2
  • 15