I have the following errors:
Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
How is this happening even though I prevent it from doing this in useEffect, as shown below
//component
function LiveStreaming({ title }) {
const [arContent, status] = useContent("story");
useEffect(() => {
if (arContent && arContent !== null) {
console.log("arContent", arContent);
}
}, [arContent]);
}
//useContent
import { useState, useEffect } from "react";
const localCache = {};
async function requestArContent() {
console.log("pinging server for content");
const res = await fetch(`${BASE_URL}/api/books`);
const json = (await res.json()) || {}; //{} so app doesn't crash if no network connection
localCache[title] = json;
}
export function useContent(title) {
const [arContent, setArContent] = useState({});
const [status, setStatus] = useState("unloaded"); //unloaded, loading, loaded
useEffect(() => {
if (!title) {
setArContent({});
} else if (localCache[title]) {
setArContent(localCache[title]);
} else {
setStatus("loading");
requestArContent();
}
}, [title]);
setArContent(localCache[title]);
setStatus("loaded");
return [arContent, status];
}