我想用React在一个表中显示一些记录,但我得到了这个错误:

无效的钩子调用。类的主体内部只能调用钩子 功能组件。这可能发生在以下情况之一 原因: React和渲染器的版本可能不匹配(比如React DOM) 你可能违反了胡克斯的规矩 你可能在同一个应用程序中有多个React副本,请参阅有关如何调试和调试的提示 解决这个问题。

import React, {
  Component
} from 'react';
import {
  makeStyles
} from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';

const useStyles = makeStyles(theme => ({
  root: {
    width: '100%',
    marginTop: theme.spacing(3),
    overflowX: 'auto',
  },
  table: {
    minWidth: 650,
  },
}));

class allowance extends Component {
  constructor() {
    super();
    this.state = {
      allowances: [],
    };

  }

  componentWillMount() {
    fetch('http://127.0.0.1:8000/allowances')
      .then(data => {

        return data.json();

      }).then(data => {

        this.setState({
          allowances: data
        });

        console.log("allowance state", this.state.allowances);
      })

  }



  render() {
    const classes = useStyles();
    return ( <
      Paper className = {
        classes.root
      } >
      <
      Table className = {
        classes.table
      } >
      <
      TableHead >
      <
      TableRow >
      <
      TableCell > Allow ID < /TableCell> <
      TableCell align = "right" > Description < /TableCell> <
      TableCell align = "right" > Allow Amount < /TableCell> <
      TableCell align = "right" > AllowType < /TableCell>

      <
      /TableRow> <
      /TableHead> <
      TableBody > {
        this.state.allowances.map(row => ( <
          TableRow key = {
            row.id
          } >
          <
          TableCell component = "th"
          scope = "row" > {
            row.AllowID
          } <
          /TableCell> <
          TableCell align = "right" > {
            row.AllowDesc
          } < /TableCell> <
          TableCell align = "right" > {
            row.AllowAmt
          } < /TableCell> <
          TableCell align = "right" > {
            row.AllowType
          } < /TableCell>                     <
          /TableRow>
        ))
      } <
      /TableBody> <
      /Table> <
      /Paper>
    );
  }

}

export default allowance;

当前回答

将其添加到package.json中

"peerDependencies": {
   "react": ">=16.8.0",
   "react-dom": ">=16.8.0"
}

来源:https://robkendal.co.uk/blog/2019-12-22-solving-react-hooks-invalid-hook-call-warning

其他回答

我遇到这个问题,我的错误原因是我开发项目A,我把另一个项目B链接到A,但是A有React包,B也有React包,它们是同一个版本(16.13)。 但是这引起了问题,我设置文件的意思是webpack.config.js,像这样:

alias: {
  'react': path.join(path.resolve(__dirname), '../node_modules/react'),
},

将B React包解析为A React包,我猜原因是一个项目不能有两个或两个以上的React包,即使它们是相同的版本。但我无法证实我的猜测。

当我链接本地图书馆时,我得到了这个错误。下面的方法解决了我的问题。

在图书馆:

从依赖中删除“react”和“react-dom”,并将它们添加到peerDependencies中。 安装依赖项,构建

重新启动主项目。

捕获这个错误:找到解决方案。

出于某种原因,我的标签上有2个onClick属性。 小心使用你或别人的自定义组件,也许其中一些已经有onClick属性。

注意导入问题——对我来说,错误是关于组件和子组件的导入/自动导入失败。与函数类与类组件无关。

这很容易发生,因为VS code自动导入可以指定的路径不能工作。 如果在组件中使用了import {MyComponent}和export default,则导入应该说import MyComponent 如果某些组件在其文件夹中使用index.js作为路径的shotcut,而其他组件则不使用,则导入可能会中断。在这里,自动导入可能会导致问题,因为它合并了来自'../../common'的{TextComponent, ButtonComponent, ListComponent}相同文件夹中的所有组件

尝试注释掉文件中给出错误的一些组件,您可以测试这是否是问题所在。

使用依赖项而不安装依赖项时也会发生。 发生在我身上,当我从“@material-ui/icons/”调用MenuIcon时,在项目中丢失了。