【问题标题】:can anyone help me with this react error?谁能帮我解决这个反应错误?
【发布时间】:2020-10-05 04:10:18
【问题描述】:

错误

第 6:44 行:在函数“app”中调用 React Hook “useState”,该函数既不是 React 函数组件也不是自定义 React Hook 函数 react-hooks/rules-of-hooks

搜索关键字以了解有关每个错误的更多信息。

【问题讨论】:

  • 请显示产生错误的代码。
  • 你是把 App 作为一个函数 App() 还是作为一个组件调用
  • const [personsState, setPersonsState ] = useState({ 人: [ {name: 'Mann', age: '20'}, {name: 'Mab', age: '25'}, {名称:'酒吧',年龄:'43'} ] });
  • @DanielPérez 是的
  • @Mab 你能显示产生错误的完整文件吗(你可以更新你的问题来显示它。)

标签: javascript reactjs react-native react-redux


【解决方案1】:

我用打字稿遇到了这个问题,将组件名称更改为以大写字母开头为我解决了这个问题。

【讨论】:

    【解决方案2】:

    你正在使用 useState 钩子,而不是在你的函数中导入 React 和 useState 钩子

    import React,{useState} from "react";
    
    //use a capital letter in the begging of your component name
    export const YourComponent = () =>{
       //Now you can use the useState hook here in a react functional component
       const [personsState, setPersonsState ] = useState({ persons: [ 
            {name: 'Mann', age:'20'}, 
            {name: 'Mab', age: '25'}, 
            {name: 'Bar', age: '43'} 
          ] })
    
       return(
         <div>
           //you can use personsState here now
         </div>
       )
    }
    

    【讨论】:

      【解决方案3】:

      create-react-app 版本 3+

      & 较新的 React 版本需要组件

      包含要大写的 React Hooks (PascalCase)。

      含义:组件名称及其导出(名称)。

      将两个(名称)的app改成App。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-26
        • 2013-07-11
        • 1970-01-01
        相关资源
        最近更新 更多