I want to show some records in a table using React but I got this error:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.
JavaScript
x
108
108
1
import React, {
2
Component
3
} from 'react';
4
import {
5
makeStyles
6
} from '@material-ui/core/styles';
7
import Table from '@material-ui/core/Table';
8
import TableBody from '@material-ui/core/TableBody';
9
import TableCell from '@material-ui/core/TableCell';
10
import TableHead from '@material-ui/core/TableHead';
11
import TableRow from '@material-ui/core/TableRow';
12
import Paper from '@material-ui/core/Paper';
13
14
const useStyles = makeStyles(theme => ({
15
root: {
16
width: '100%',
17
marginTop: theme.spacing(3),
18
overflowX: 'auto',
19
},
20
table: {
21
minWidth: 650,
22
},
23
}));
24
25
class allowance extends Component {
26
constructor() {
27
super();
28
this.state = {
29
allowances: [],
30
};
31
32
}
33
34
componentWillMount() {
35
fetch('http://127.0.0.1:8000/allowances')
36
.then(data => {
37
38
return data.json();
39
40
}).then(data => {
41
42
this.setState({
43
allowances: data
44
});
45
46
console.log("allowance state", this.state.allowances);
47
})
48
49
}
50
51
52
53
render() {
54
const classes = useStyles();
55
return ( <
56
Paper className = {
57
classes.root
58
} >
59
<
60
Table className = {
61
classes.table
62
} >
63
<
64
TableHead >
65
<
66
TableRow >
67
<
68
TableCell > Allow ID < /TableCell> <
69
TableCell align = "right" > Description < /TableCell> <
70
TableCell align = "right" > Allow Amount < /TableCell> <
71
TableCell align = "right" > AllowType < /TableCell>
72
73
<
74
/TableRow> <
75
/TableHead> <
76
TableBody > {
77
this.state.allowances.map(row => ( <
78
TableRow key = {
79
row.id
80
} >
81
<
82
TableCell component = "th"
83
scope = "row" > {
84
row.AllowID
85
} <
86
/TableCell> <
87
TableCell align = "right" > {
88
row.AllowDesc
89
} < /TableCell> <
90
TableCell align = "right" > {
91
row.AllowAmt
92
} < /TableCell> <
93
TableCell align = "right" > {
94
row.AllowType
95
} < /TableCell> <
96
/TableRow>
97
))
98
} <
99
/TableBody> <
100
/Table> <
101
/Paper>
102
);
103
}
104
105
}
106
107
export default allowance;
108
Advertisement
Answer
You can only call hooks from React functions. Read more here.
Just convert the Allowance
class component to a functional component.
JavaScript
1
67
67
1
const Allowance = () => {
2
const [allowances, setAllowances] = useState([]);
3
4
useEffect(() => {
5
fetch("http://127.0.0.1:8000/allowances")
6
.then(data => {
7
return data.json();
8
})
9
.then(data => {
10
setAllowances(data);
11
})
12
.catch(err => {
13
console.log(123123);
14
});
15
}, []);
16
17
const classes = useStyles();
18
return ( <
19
Paper className = {
20
classes.root
21
} >
22
<
23
Table className = {
24
classes.table
25
} >
26
<
27
TableHead >
28
<
29
TableRow >
30
<
31
TableCell > Allow ID < /TableCell> <
32
TableCell align = "right" > Description < /TableCell> <
33
TableCell align = "right" > Allow Amount < /TableCell> <
34
TableCell align = "right" > AllowType < /TableCell> <
35
/TableRow> <
36
/TableHead> <
37
TableBody > {
38
allowances.map(row => ( <
39
TableRow key = {
40
row.id
41
} >
42
<
43
TableCell component = "th"
44
scope = "row" > {
45
row.AllowID
46
} <
47
/TableCell> <
48
TableCell align = "right" > {
49
row.AllowDesc
50
} < /TableCell> <
51
TableCell align = "right" > {
52
row.AllowAmt
53
} < /TableCell> <
54
TableCell align = "right" > {
55
row.AllowType
56
} < /TableCell> <
57
/TableRow>
58
))
59
} <
60
/TableBody> <
61
/Table> <
62
/Paper>
63
);
64
};
65
66
export default Allowance;
67