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

当前回答

我的情况… 钩子中的解决方案

const [cep, setCep] = useState('');
const mounted = useRef(false);

useEffect(() => {
    if (mounted.current) {
        fetchAPI();
      } else {
        mounted.current = true;
      }
}, [cep]);

const setParams = (_cep) => {
    if (cep !== _cep || cep === '') {
        setCep(_cep);
    }
};

其他回答

当我使用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修复了这个问题。

我用的是电子。 我测试了上面所有的答案,甚至深入到基础应用程序,发现这种情况仍然存在。

事实证明,如果你正在使用electron(在我的情况下,特别是expo-electron),它有它自己的webpack配置,并且你需要在webpack配置中白名单redux。我的electronic -webpack.js文件是这样的:

const { withExpoAdapter } = require("@expo/electron-adapter");

// Provide any overrides for electron-webpack: https://github.com/electron-userland/electron-webpack/blob/master/docs/en/configuration.md
module.exports = withExpoAdapter({
  projectRoot: __dirname,
  whiteListedModules: ["react-redux"],
});

当你以错误的方式声明useDispatch from react-redux时,也会发生此错误: 当你执行:const dispatch = useDispatch而不是:const dispatch = useDispatch();(也就是说要记得加上圆括号)

在我的情况下,我在package-json中所做的更改导致了问题。

npm install react-redux

解决这个问题

我的共享库之间不同版本的react似乎是问题所在(16和17),将两者都更改为16。