所以我们都知道我们不能像文档所说的那样将 React hooks 放在条件语句之后或之中。
function MyComponent(...) {
if (condition) {
React.useState() // not allowed
}
return <div></div>;
}
function MyComponent(...) {
if (condition) {
return null;
}
React.useState() // also not allowed
return <div></div>;
}
Run Code Online (Sandbox Code Playgroud)
使用 React hooks 的 eslint 插件,我们会收到构建错误,指出上述内容是不允许的。
但为什么插件没有响应这个
function MyComponent(...) {
if (errorCondition) {
throw Error;
}
React.useState() // allowed???
return <div></div>;
}
Run Code Online (Sandbox Code Playgroud)
这是不允许的,并且插件没有更新来捕获这个吗?或者这实际上是允许的?文档似乎没有提到这一点