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

当前回答

当我使用npm link安装我的本地库时,我遇到了这个问题,我使用cra构建了本地库。我在这里找到了答案。字面意思是:

当你使用npm link或类似的工具时,这个问题也会出现。 在这种情况下,您的捆绑器可能“看到”两个react -一个在应用程序中 文件夹和图书馆文件夹里的一个。假设'myapp'和'mylib' 是兄弟文件夹,一个可能的解决办法是运行'npm link . ./myapp/node_modules/react这应该使 库使用应用程序的React副本。

因此,运行命令:npm link <path_to_local_library>/node_modules/react,例如。在我的情况下NPM链接..项目目录下的/libraries/core/decipher/node_modules/react修复了这个问题。

其他回答

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

将其添加到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

在我的情况下,我拆除了包装锁。Json和node_modules从两个项目和重新安装,现在工作得很好。


// project structure


root project
- package-lock.json
- package.json // all dependencies are installed here
- node_modules

-- second project
-- package-lock.json
-- package.json 
  "dependencies": {
    "react": "file:../node_modules/react",
    "react-dom": "file:../node_modules/react-dom",
    "react-scripts": "file:../node_modules/react-scripts"
  },
-- node_modules


不确定是什么原因导致了这个问题,因为这发生在我之前,并采取了与上面相同的步骤,问题已经解决了。

这是我解决问题的方法。我有node_modules文件夹和files包。Json和包锁。Json在我的组件文件夹以及在我的项目的根它所属的地方。我把它们从不属于它们的地方删除了。不要问我做了什么把它们放在那里,我一定是在错误的位置做了一个npm。

出现这种情况的原因有很多。其中之一是,当包的安装超出范围时。它容易混淆两个react应用程序。

解释:

这些包不应该安装在不同的级别。

这个错误

app
 |node_modules          <-- some packages installed here
 |package.json
node_modules
package.json            <-- some packages installed here

正确的方法

app
 |node_modules
 |package.json          <-- install all the packages at the same heirarchy