I have a two-layer application.
The API-App offers the bare data as JSON whereas the React-App is only used for presentation.
To get those data from the API, I'm want to load and static config.json in the public folder and fetch the data from the given URL or the default fallback, called example.json.
#config.json
{
"api_url": "example.json"
...
}
By running the react-App, I want to do the following:
- Load the config.json once
- Save the api_url as State
- Initially load the data from the API by using the api_url-state
- Iterate every delay (e.g. 4secs) -> reload API data and refresh the presented content
function App()
const [delay] = useState(4000);
const [content, setContents] = useState<Array<Content>>([]);
const [apiurl, setApiurl] = useState("");
fetch(window.location.href + "config.json")
.then((respone) => respone.json())
.then((e) => setApiurl(e.API_URL));
useEffect( () => {
fetch(apiurl)
.then((response) => response.json())
.then((element) => setContent(element));
}, [apiurl])
useEffect(() => {
document.title = "Confview-Frontend";
function tick() {
fetch(apiurl)
.then((response) => response.json())
.then((element) => setContent(element));
}
const intervalId = setInterval(tick, delay);
return () => clearInterval(intervalId);
}, [delay, apiurl]);
Unfortunately, the given code creates some sort of an infinity loop.
What am I missing to get the described behavior?