I removed the imports because they are not a problem
In my render, I set my state and it shows it to me in the console, but when I try to map it, it is null and it says to me that there are no elements in the state "allInfo", how can I fix this error so that my state updates?
class Friends extends Component {
constructor(props) {
super(props)
this.state = {
allInfo: []
}
}
render() {
fetch('https://localhost:44314/api/Users')
.then(response => response.json())
.then(data => {
this.setState(prevState => {
allInfo: data.map((obj) => { prevState.allInfo.push(obj) })
})
})
return (
<div>
<NavMenu />
<div style={{ fontSize: 38, textAlign: "center", color: "white" }}>FRIENDS</div>
<TableContainer component={Paper}>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Users</TableCell>
<TableCell align="right">Follow</TableCell>
</TableRow>
</TableHead>
<TableBody>
{this.state.allInfo.map((row) => (
<TableRow key={row.username}>
<TableCell component="th" scope="row">
{row.username}
</TableCell>
<TableCell align="right">Follow?</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</div>
);
} }
export default Friends