前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React报错之React Hook 'useEffect' is called in function

React报错之React Hook 'useEffect' is called in function

作者头像
chuckQu
发布2022-09-20 11:36:25
1.2K0
发布2022-09-20 11:36:25
举报
文章被收录于专栏:前端F2E

总览

为了解决错误"React Hook 'useEffect' is called in function that is neither a React function component nor a custom React Hook function",可以将函数名的第一个字母大写,或者使用use作为函数名的前缀。比如说,useCounter使其成为一个组件或一个自定义钩子。

react-hook-useeffect-called-in-function.png

这里有个示例用来展示错误是如何发生的。

代码语言:javascript
复制
// App.js

import React, {useEffect, useState} from 'react';

// 👇️ Not a component (lowercase first letter)
// not a custom hook (doesn't start with use)
function counter() {
  const [count, setCount] = useState(0);

  // ⛔️ React Hook "useEffect" is called in function "counter" that
  // is neither a React function component nor a custom React Hook function.
  // React component names must start with an uppercase letter.
  // React Hook names must start with the word "use".
  useEffect(() => {
    console.log(count);
  }, [count]);

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

上述代码片段的问题在于,我们在一个函数中使用了useEffect钩子,而这个函数不是一个组件,因为它以小写字母开头,也不是一个自定义钩子,因为它的名字不是以use开头。

声明组件

如果你想声明一个组件,请将你的函数的第一个字母大写。

代码语言:javascript
复制
// App.js

import React, {useEffect, useState} from 'react';

// 👇️ is now a component (can use hooks)
function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log(count);
  }, [count]);

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default function App() {
  return (
    <div>
      <Counter />
    </div>
  );
}

函数名必须以大写字母开头,因为这有助于React区分诸如pdivspan等内置元素和我们定义的组件。

就像文档中所说的:

  • 只从React函数组件或自定义钩子中调用Hook
  • 只在最顶层使用 Hook
  • 不要在循环,条件或嵌套函数中调用 Hook
  • 确保总是在你的 React 函数的最顶层以及任何 return 之前使用 Hook

声明自定义钩子

如果你想声明一个自定义钩子,自定义钩子的名称必须以use开头,比如说useCounter

代码语言:javascript
复制
import React, {useEffect, useState} from 'react';

// 👇️ is a custom hook (name starts with use)
function useCounter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log(count);
  }, [count]);

  return [count, setCount];
}

export default function App() {
  const [count, setCount] = useCounter();

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

自定义钩子的名字必须以use开头,这样React才能识别你的函数是一个自定义钩子。

总结

为了解决"React Hook 'useEffect' is called in function that is neither a React function component nor a custom React Hook function"的错误,确保只从React函数组件或自定义钩子中调用钩子。

参考资料

[1]

https://bobbyhadz.com/blog/react-hook-useeffect-is-called-in-function-that-is-neither: https://bobbyhadz.com/blog/react-hook-useeffect-is-called-in-function-that-is-neither

[2]

Borislav Hadzhiev: https://bobbyhadz.com/about

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-08-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端F2E 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 总览
  • 声明组件
  • 声明自定义钩子
  • 总结
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档