Skip to content
← Back to rules

react/no-is-mounted Correctness

它的作用

此规则禁止在类组件中使用 isMounted

为什么这不好?

isMounted 是一种反模式,在使用类组件或函数组件时不可用。

示例

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

jsx
class Hello extends React.Component {
  someMethod() {
    if (!this.isMounted()) {
      return;
    }
  }
  render() {
    return <div onClick={this.someMethod.bind(this)}>Hello</div>;
  }
}

如何使用

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

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

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

版本

此规则于 v0.0.19 中添加。

参考资料