I am having an issue with fetching data inside an useEffect(). I can see in the developer tool, that the fetch is called in an infite loop.
I am quite new to React, so maybe this is a simple issue. I have tried looking up others with same issues, but can't seem to make a connection with my code.
Code is below.
export function Category(){
const [categories, setCategories] = useState([]);
useEffect(() => {
fetch('https://localhost:5001/api/Category')
.then(response => response.json())
.then(data =>
setCategories(data));
return () => {
}
})
const renderCategories = (cats) => {
return (
<Table ClassName= "mt-4" striped bordered hoved size= "=sm">
<thead>
<tr>
<th> Id: </th>
<th> Category Name: </th>
<th> Options </th>
</tr>
</thead>
<tbody>
{cats.map(cats=>
<tr key = {cats.categoryId}>
<td> {cats.categoryId}</td>
<td> {cats.name}</td>
</tr>)}
</tbody>
</Table>
)
}
return(
<div>
{renderCategories(categories)}
</div>
)
}