错误:使用 antd 库时无效的钩子调用

发布于 2025-01-15 23:40:39 字数 3185 浏览 1 评论 0原文

我尝试在 React 中使用 antd 库的输入,它显示

错误:无效的挂钩调用。钩子只能在主体内部调用 此行中的函数组件的 \setIsLoaded(true)

我尝试更新 React 并且 hooks 全部位于顶层。我该如何解决这个问题?

如果我删除此行 ,则代码可以完美运行。

import React , { useState,useEffect } from 'react';
import ReactDOM from 'react-dom';
import {Input} from 'antd';
import './style.scss';



const header = ["Name", "User Name", "Email", "Number Of Albums"];
 

function App(){

const { Search } = Input;
const [error, setError] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);
const [items, setItems] = useState([]);


    
 useEffect(() => {
    fetch("https://jsonplaceholder.typicode.com/users").then((res) => res.json()).then(
           (result) => {
                     setIsLoaded(true);
                     setItems(result);
                    },
           (error) => {
                      setIsLoaded(true);
                      setError(error);
                    }
                );
        }, []);
        
 
    
    if (error) {
            return <>{error.message}</>;
          } else if (!isLoaded) {
             return <>loading...</>;
          } else {
            
 
return(

<div className="w">
<div className="col-2">

<Search placeholder="input search text" allowClear style={{ width: 200 }} />

    
    
   </div>
 
   <table>
    <thead>
      <tr>
        {header.map((h, i) => <th key={i}>{h}</th>)}
       </tr>
    </thead>
    <tbody>
    
       {
         items.map((item) => {
         return (
         <tr key={item.id}>
              
          <td>{item.name}</td>
           <td>{item.username}</td>
           <td>{item.email}</td>
          
           
          </tr>

              
            );
          })}
          
     </tbody>
     </table>

        
    </div>




 
        )}
        

}


ReactDOM.render(<App/>, document.getElementById("col-3"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

  
  <link rel="stylesheet" type="text/css" href="style.css"/>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
  

</head>
   
  
<body>
   
<div class="row">

 <div id="col-1"><h1>User gallery</h1></div>
 
 
 <div id="col-3">
  <div id="tableContainer">
 
  </div>
 </div>
 
</div>
    
    
</body>
</html>

I try to use input from antd library in react it shows

Error: Invalid hook call. Hooks can only be called inside of the body
of a function component in this line \setIsLoaded(true)

I tried to update react and hooks is all in top level. How can I solve this problem?

If I delete this line <Search placeholder="input search text" allowClear style={{ width: 200 }} />, the code works perfectly.

import React , { useState,useEffect } from 'react';
import ReactDOM from 'react-dom';
import {Input} from 'antd';
import './style.scss';



const header = ["Name", "User Name", "Email", "Number Of Albums"];
 

function App(){

const { Search } = Input;
const [error, setError] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);
const [items, setItems] = useState([]);


    
 useEffect(() => {
    fetch("https://jsonplaceholder.typicode.com/users").then((res) => res.json()).then(
           (result) => {
                     setIsLoaded(true);
                     setItems(result);
                    },
           (error) => {
                      setIsLoaded(true);
                      setError(error);
                    }
                );
        }, []);
        
 
    
    if (error) {
            return <>{error.message}</>;
          } else if (!isLoaded) {
             return <>loading...</>;
          } else {
            
 
return(

<div className="w">
<div className="col-2">

<Search placeholder="input search text" allowClear style={{ width: 200 }} />

    
    
   </div>
 
   <table>
    <thead>
      <tr>
        {header.map((h, i) => <th key={i}>{h}</th>)}
       </tr>
    </thead>
    <tbody>
    
       {
         items.map((item) => {
         return (
         <tr key={item.id}>
              
          <td>{item.name}</td>
           <td>{item.username}</td>
           <td>{item.email}</td>
          
           
          </tr>

              
            );
          })}
          
     </tbody>
     </table>

        
    </div>




 
        )}
        

}


ReactDOM.render(<App/>, document.getElementById("col-3"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

  
  <link rel="stylesheet" type="text/css" href="style.css"/>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
  

</head>
   
  
<body>
   
<div class="row">

 <div id="col-1"><h1>User gallery</h1></div>
 
 
 <div id="col-3">
  <div id="tableContainer">
 
  </div>
 </div>
 
</div>
    
    
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2025-01-22 23:40:39

使用 $npm run build 构建 antd 包

Use $npm run build to build antd package

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文