I am trying to use a custom hook to fetch data and pass it to a component. However, the component ends up rendering multiple times (going beyond 100), as shown in the image below
This is the component that consumes the custom hook (userId) is a prop that is handed down from the parent component
export default function AuthorDetails({ userId }) {
const { articleAuthor, authorError, authorLoading } = useGetAuthor(userId);
return (
<div className="authordetails__container">
<div className="authorDetails__content">
{console.log(articleAuthor)}
{authorLoading && <p>Loading...</p>}
{articleAuthor && (
<p>
<span>{articleAuthor.author}</span>
{articleAuthor.twitter && (
<span>
<img src={twittericon} alt="twitter" />
</span>
)}
{articleAuthor.facebook && (
<span>
<img src={facebookicon} alt="facebook" />
</span>
)}
{articleAuthor.website && (
<span>
<img src={websiteicon} alt="website" />
</span>
)}
</p>
)}
{authorError && <p>{authorError}</p>}
</div>
</div>
);
}
This is the code for the custom hook
export default function useGetAuthor(userId) {
const { currentUser } = useAuth();
const [articleAuthor, setArticleAuthor] = useState();
const [authorError, setAuthorError] = useState();
const [authorLoading, setAuthorLoading] = useState(false);
useEffect(() => {
async function getAuthor() {
try {
setAuthorLoading(true);
if (currentUser && currentUser.id === userId) {
setArticleAuthor(currentUser);
setAuthorLoading(false);
} else {
const data = await database.users.doc(userId).get();
setArticleAuthor(database.formatDocument(data));
setAuthorLoading(false);
}
} catch (err) {
setAuthorLoading(false);
console.log(err);
setAuthorError("Something went wrong! Please refresh to try again");
}
}
getAuthor();
});
return { articleAuthor, authorLoading, authorError };
}

console.log(articleAuthor)into auseEffectwithout the dependency so you log once peruseEffectcall per render cycle, then track how many unexpected rerenders occur.console.log(articleAuthor)in the useEffect without the dependency. It does indeed show that the component is rendering multiple times