【发布时间】:2019-10-17 23:36:22
【问题描述】:
有this section on React Hooks我不太明白它在说什么:
仅来自 React 函数的调用挂钩
不要从常规 JavaScript 函数中调用 Hooks。相反,您可以:
✅ 从 React 函数组件调用 Hooks。
✅ 从自定义 Hooks 调用 Hooks(我们将在下一页了解它们)。
通过遵循此规则,您可以确保组件中的所有有状态逻辑在其源代码中都清晰可见。
仅从 React 函数组件调用 Hook 是什么意思?React 函数与我所说的常规函数组件有何不同?
在我看来它们是一样的:
const App = () => {
useEffect(() => //do stuff);
return (
// return some sort of stuff here
)
}
我问的原因是我对钩子的 eslint 抱怨我在这里使用useState 的方式:
const checkPermissions = () => { //when I change this to 'usePermissions', it works fine
const [locPermission, setLocPermission] = useState(null); // eslint error here
//'React Hook "useState" is called in function "checkPermissions" which
//is neither a React function component or a custom React Hook function.'
//Check for and set state for permissions here
return locPermission;
};
【问题讨论】:
-
当您将
checkPermissions添加到任何“React 函数”(任何返回 React 元素的函数)时,eslint 可能会停止抱怨。我同意这是令人困惑的术语。
标签: reactjs react-native react-hooks