简体   繁体   English

如何解决 eslint 错误:自定义路由组件中的“禁止传播道具”?

[英]How to resolve eslint error: "prop spreading is forbidden" in a custom route component?

How can I resolve the eslint error: "prop spreading is forbidden" in a custom route component?如何解决自定义路由组件中的 eslint 错误:“禁止传播道具”?

This error occurs on lines 3 and 6 below:此错误发生在下面的第 3 行和第 6 行:

const PrivateRoute = ({component: Component, ...rest}) => (
  <Route
    {...rest}
    render={(props) => (
        localStorage.getItem('user') ?
          <Component {...props} /> :
          <Redirect to={{pathname: '/login', state: {from: props.location}}} />
  )}
  />
);

For ESLint is important which type of comment do you use, the legit one is:因为 ESLint 很重要,你使用哪种类型的注释,合法的是:

/* eslint-disable react/jsx-props-no-spreading */

ES lint discourages the use of prop spreading so that no unwanted/unintended props are being passed to the component. ES lint 不鼓励使用 prop 传播,以便不会将不需要的/意外的 props 传递给组件。 More details here: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md更多细节在这里: 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.要为特定文件禁用它,您可以将: /* eslint-disable react/jsx-props-no-spreading */放在组件文件的第一行。 For disabling it for all files, try this: Disable in EsLint "react / jsx-props-no-spreading" error in Reactjs要为所有文件禁用它,请尝试以下操作: Disable in EsLint "react / jsx-props-no-spreading" error in Reactjs

Edited comments as per answer below根据下面的答案编辑评论

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.如果您使用的是 vscode 并安装了 eslint 插件,那么只需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.这样做会自动添加正确的 lint 注释。

在此处输入图像描述

You can use this one just to comment out the below line:-您可以使用它来注释以下行:-

/* eslint-disable-next-line react/jsx-props-no-spreading */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM