无效的钩子调用,虽然钩子调用是在函数体?

  • 本文关键字:调用 函数体 无效 reactjs
  • 更新时间 :
  • 英文 :

Unhandled Rejection (Error): Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
我不明白我为什么要违反规则。此时应该允许钩子调用。谁能告诉我为什么不接电话?
export const createMail =
(values: ValuesType): AppThunk =>
async (dispatch, getState) => {
const state = getState();
const { t } = useTranslation(["emails", "translation"]);

发生这种情况的原因是错误中提到的第二个原因。你只能在函数组件内部使用react钩子(useStates...)。这里不仅不是一个函数组件(因为camelCase),而且在前面提到的函数返回的另一个函数中使用了useState。

最新更新