I'm trying to send a GET request to http://localhost:8080/api/RE/template/${pageId}
to display template items that belong to a specific id of the page. This is what displays on the console,
TemplateRealEstate.js
const TemplateRealEstate = () => {
const [loadedTemplates, setLoadedTemplates] = useState([])
const pageId = useParams().pageId;
const getTemplates = async () => {
await axios({
method: "GET",
url: `http://localhost:8080/api/RE/template/${pageId}`,
headers: "Content-type: application/json"
})
.then((res) => {
console.log("Respond from the request -->", res)
setTimeout(() => setLoadedTemplates(res.data.templates), 0);
console.log("Res.data.templates -->", res.data.templates)
})
.catch((err) => {
console.log(err)
})
console.log("Loaded Templates --> ", loadedTemplates)
}
useEffect(() => {
getTemplates();
}, [pageId])
}
So far I tried, using useCallback
, setTimeout
. I tried to add loadedTemplates as a dependency to useEffect
to render the page but then it renders infinite. It displays literally infinite console.logs. If I leave the dependencies empty, it still continues the same.
Since I have files in the form, normally I make a POST request as 'Content-Type': 'multipart/form-data'
. I've also tried to change the headers in the GET request but still the same. Why this is happening? I've other GET requests that didn't have this problem at all.