React Hooks 规则
注意: 此规则包含在以下规则集中:
react
fresh
在
deno.json
中启用完整规则集{ "lint": { "tags": ["react"] // ...or "fresh" } }
使用 Deno CLI 启用完整规则集
deno lint --tags=react # or ... deno lint --tags=fresh
确保在 React/Preact 组件中正确调用 Hooks。它们必须在组件的顶层调用,而不是在条件语句或循环内部调用。
无效
// WRONG: Called inside condition
function MyComponent() {
if (condition) {
const [count, setCount] = useState(0);
}
}
// WRONG: Called inside for loop
function MyComponent() {
for (const item of items) {
const [count, setCount] = useState(0);
}
}
// WRONG: Called inside while loop
function MyComponent() {
while (condition) {
const [count, setCount] = useState(0);
}
}
// WRONG: Called after condition
function MyComponent() {
if (condition) {
// ...
}
const [count, setCount] = useState(0);
}
有效
function MyComponent() {
const [count, setCount] = useState(0);
}