React Hook “useState“ is called in function “xxxx“ which is neither a React function component

报错

React Hook “useState” is called in function “search” which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
React Hook “useState“ is called in function “xxxx“ which is neither a React function component

原因分析

  1. 报错提示search函数既不是一个React组件,也不是一个Hook函数。
  2. React中定义组件有两种方式:
    1). 组件名首字母大写,如:Search
    2). 自定义Hook函数使用use开头,如:useSearch

解决方法

  1. 将组件名首字母大写
  2. 自定义hook时,函数名前缀加use
上一篇:Rpc避免流量打到没有启动完全的节点上


下一篇:学习React之扩展相关