gpt4 book ai didi

javascript - 无效的 Hook 调用。钩子(Hook)只能在函数组件体内调用

转载 作者:行者123 更新时间:2023-12-03 13:03:30 24 4
gpt4 key购买 nike

我想使用 React 在表中显示一些记录,但出现此错误:

Invalid hook call. Hooks can only be called inside of the body of afunction component. This could happen for one of the followingreasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See for tips about how to debug andfix this problem.
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 构建的本地库。我找到了答案here 。字面意思是:

This problem can also come up when you use npm link or an equivalent.In that case, your bundler might “see” two Reacts — one in applicationfolder and one in your library folder. Assuming 'myapp' and 'mylib'are sibling folders, one possible fix is to run 'npm link../myapp/node_modules/react' from 'mylib'. This should make thelibrary use the application’s React copy.

因此,运行命令:npm link <path_to_local_library>/node_modules/react ,例如。就我而言npm link ../../libraries/core/decipher/node_modules/react从项目目录修复了该问题。

关于javascript - 无效的 Hook 调用。钩子(Hook)只能在函数组件体内调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com