我想用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;

当前回答

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

在图书馆:

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

重新启动主项目。

其他回答

React linter假定每个方法都以使用钩子开始,并且钩子不能在类中工作。通过将const usstyles重命名为任何不以use开头的东西,比如const myStyles,就可以了。

更新:

makestyle是钩子api,你不能在类中使用它。你可以使用样式组件API。在这里看到的

如果上面所有的都不能工作,特别是如果有很大的大小依赖(就像我的情况),构建和加载都至少需要15秒,所以延迟似乎给出了一个错误的消息“无效钩子调用”。 因此,您所能做的就是在测试之前留出一些时间来确保构建已经完成。

在我的情况下,我试图在窗户上使用mdbreact。虽然它安装了,但我得到了上述错误。我不得不重新安装,一切正常。我曾经在antd图书馆遇到过一次

可能发生此错误的另一个原因是,如果声明函数组件时使用了箭头函数签名而不是函数签名。

例: 从箭头函数更改函数组件声明

export const Counter = (props) => {}

TO函数声明

export function Counter (props) {}

这将有助于解决问题。 至少对我来说是这样。

当我在类组件中使用useLocation钩子时,我也有同样的问题

错误:

import React from "react";
import { useLocation } from "react-router-dom";

class ShowTheLocation extends React.Component {
  const location = useLocation();
  render() {
    return <div>You are now at {location.pathname}</div>;
  }
}

解决方案:我已经将类组件转换为功能组件,然后问题解决

import React from "react";
import { useLocation } from "react-router-dom";

const ShowTheLocation = () => {
  const location = useLocation();

  return <div>You are now at {location.pathname}</div>;
}