我用react + ant design做前端。在使用ant design组件时一直遇到如下问题:
react.development.js:1476 Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
import { Component } from 'react';
/* import { Button } from "antd"; */
import { Table } from 'antd';
import "antd/dist/antd.min.css";
class GoodsList extends Component{
state = {}; //存储状态
componentDidMount() {
const dataSource = [
{
key: '1',
name: '胡彦斌',
age: 32,
address: '西湖区湖底公园1号',
},
{
key: '2',
name: '胡彦祖',
age: 42,
address: '西湖区湖底公园1号',
},
];
this.setState({dataSource})
}
render() {
const columns = [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
},
{
title: '住址',
dataIndex: 'address',
key: 'address',
},
];
return <Table dataSource={this.state.dataSource} columns={columns} />;
}
}
export default GoodsList;
import GoodsList from "./views/GoodsList";
function App() {
return (
<div>
<GoodsList>GoodsList>
div>
);
}
export default App;
import React from 'react'
import ReactDOM from 'react-dom'
import App from "./App"
ReactDOM.render(<App />, document.getElementById("root"))
我用的教程是react17 然后我下载的是18,但已经降成了17。 根据教程来编写会出现此类错误。根据ant design官方文档来编写也还是出现此类错误。 网上查了各种方法,但都不管用。
不出现截图中的错误
你参照这个实际案例,看看是否对你有所帮助:https://blog.csdn.net/qq_45327886/article/details/109129999
我看了下,和你的报错类似,解决方法看是否对你有所启发
万能的删除node_modual目录,重新npm install后执行试过了吗?
在删除 React 版本时,未完全删除。
在 你的项目中存在多个React 版本。
react和react-dom都将到17版本,删除掉node_modules,重新npm install