簡體   English   中英

誰能幫我解決這個反應錯誤?

[英]can anyone help me with this react error?

錯誤

Line 6:44: React Hook "useState" 在 function "app" 中被調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385-of41AB5074C1

搜索關鍵字以了解有關每個錯誤的更多信息。

我用 typescript 遇到了這個,將組件名稱更改為以大寫字母開頭為我解決了這個問題。

您正在使用 useState 掛鈎,而不是在 function 中導入 React 和 useState Hook

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+

& 較新的 React 版本需要組件

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

含義:組件名稱及其導出(名稱)。

將兩個(名稱)應用程序更改為 App。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM