I have a react app where I try to fetch some functions from a js file:
useEffect(() => {
const todayDate = new Date();
fetch("https://raw.githubusercontent.com/courseraap/capstone/main/api.js")
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.text();
})
.then((textResponse) => {
console.log("textResponse :>> ", textResponse);
const scriptFunction = new Function(textResponse);
scriptFunction();
window.fetchAPI(todayDate); // error, it is not a function. window.fetchAPI is undefined
});
}, []);
I’ve read that if I call the response this way it will add those function objects to the window object, which is not happening for me for some reasons. What am I missing here?
The url is valid, so it can be checked the function is there.