Skip to content
← Back to rules

react/no-danger Restriction

它的作用

此规则禁止使用 dangerouslySetInnerHTML 属性。

这为什么不好?

dangerouslySetInnerHTML 是一种将 HTML 注入到你的 React 组件中的方式。这很危险,因为它很容易导致 XSS 漏洞。

示例

以下是此规则的错误代码示例:

jsx
import React from "react";

const Hello = <div dangerouslySetInnerHTML={{ __html: "Hello World" }}></div>;

以下是此规则的正确代码示例:

jsx
import React from "react";

const Hello = <div>Hello World</div>;

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["react"],
  "rules": {
    "react/no-danger": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["react"],
  rules: {
    "react/no-danger": "error",
  },
});
bash
oxlint --deny react/no-danger --react-plugin

版本

此规则于 v0.0.14 中添加。

参考资料