简体   繁体   English

如果使用 useState,如何将 es-lint 配置为警告/错误

[英]How can es-lint be configured to warn/error if useState is used

I've not been able to find a rule, but is it possible to throw a warning or an error if someone attempts to reach for useState instead of the state provider?我无法找到规则,但是如果有人尝试访问useState而不是 state 提供程序,是否可以抛出警告或错误?

I'm hoping I don't need to reach for a custom rule, but can work through that if there isn't an existing one out there.我希望我不需要达到自定义规则,但如果那里没有现有规则,我可以解决这个问题。

You can use no-restricted-syntax您可以使用无限制语法

All you need to do is play around with AST Explorer .您需要做的就是玩弄AST Explorer I have done it for you and you need to add this to your .eslintrc file in rules section.我已经为您完成了,您需要将其添加到rules部分的.eslintrc文件中。

In the example below depending upon your need you can either throw it as an error or warn .在下面的示例中,根据您的需要,您可以将其作为errorwarn抛出。

"no-restricted-syntax": [
     "error",
     {
         "selector": "CallExpression[callee.name='useState']",
         "message": "Please use state provider instead."
     }
],

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

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