简体   繁体   English

eslint / eslint-plugin-react:<form action="Javascript:void(0)"> - 无脚本 url 错误</form>

[英]eslint / eslint-plugin-react: <form action="Javascript:void(0)" > - no-script-url error

I have a component with a form:我有一个带有表单的组件:

< form action="javascript:void(0)" onSubmit={this.join}> <form action="javascript:void(0)" onSubmit={this.join}>

Eslint is complaining:埃斯林特抱怨:

error Script URL is a form of eval no-script-url错误脚本 URL 是 eval no-script-url 的一种形式

Note: I am also using "eslint-plugin-react"注意:我也在使用“eslint-plugin-react”

How can I relax this rule or what would be an alternative to the javascript void function?我该如何放宽这条规则,或者什么可以替代 javascript void function?

I was having just this problem and then saw this pattern in the official Redux docs and it made a lot of sense to me:我遇到了这个问题,然后在官方 Redux 文档中看到了这种模式,这对我来说很有意义:

<a
  href=""
  onClick={e => {
    e.preventDefault()
    onClick()
  }}
>
  {children}
</a>

Source来源

That's what I'll be doing from now on.这就是我从现在开始要做的事情。

我禁用了它:

"no-script-url": 0

Actually it is wrong to disabled it, because javascript: is like eval for browser, and eval is evil as people know.实际上禁用它是错误的,因为javascript:就像浏览器的 eval 一样,而众所周知 eval 是邪恶的。

Alternatively, you don't need to put action prop.或者,您不需要放置action道具。 preventDefault in your join method instead.改为在您的join方法中使用preventDefault For example;例如;

function join(e){
  e.preventDefault();
  //...
}

e.preventDefault() change with javascript:void(0) e.preventDefault()更改为javascript:void(0)

<form action={
    e => {
        e.preventDefault()
    }
} onSubmit={this.join}>
 //...   
</form>

Or add rule in package.json file或者在package.json文件中添加规则

"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "rules": {
       //...
      "no-script-url": "off",
       //...
    }
  },

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

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