I can't get this to work unfortunately and would very much appreciate some help :)
I get an array via POST request and want the elements of the array to be rendered in a list. I get the array, but it is not updating the useState as intended.
import React, { useEffect, useState } from "react";
const ShowFiles = () => {
const [list, setList] = useState([])
useEffect( () => {
fetch("https://--------------/getFiles", {
headers: {
'Content-Type': 'application/json'},
method: 'POST',
crossDomain:true,
})
.then( results => results.json())
.then(data => {
console.log(data)
console.log("items:", data)
setList(data)
console.log("list:", list)
})
},[])
return (
<div>
{list.map(({item}) => (
<ul>{item}</ul>
))}
</div>
)
}
In the above example the list-variable stayed empty.
So I learned that useEffect needs a dependency variable, so I added "list" as such, which passed the array to the list-variable but it wouldn't stop making the POST request, so also not a solution.
Any idea in which direction I can get this to work?