报错信息
- Line 5:25: React Hook “useState” is called in function “addABC” 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”
错误原因
- 错误原因: addABC 函数中使用了 useState,而 useState 是 React 的钩子(hook),只能在 React 函数组件或自定义钩子中使用,不能在普通函数中使用。
- 过度或不恰当使用可能会导致代码难以理解或维护。工具函数(如处理 IndexedDB 的函数)应该独立于 React,避免与组件的生命周期紧密耦合。
处理错误
- 一般需要将原本的Hook内容修改掉,放入
**Utils.js
中,然后export { FUNCNAME };
供其他文件使用。 - 或者将函数addABC修改成useaddABC(或AddABC)变为Hook