Next.js Discord

Discord Forum

Infinite Re-Renders in Client Component

Answered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
Hi, for some reason my component is re-rendering infinitely, and the useEffect() hook is being invoked about twice a second infinitely.

Any ideas why, and how I could resolve this?

'use client';
import axios from 'axios';
import { useState, useEffect } from 'react';

export default function Home() {
  const [groups, setGroups] = useState(null);

  useEffect(() => {
    console.log('use effect!');
    axios.get('http://localhost:8080/api/groups', { withCredentials: true }).then((res) => {
      setGroups(res.data);
    });
  });

  console.log('GROUPS:', groups);

  return <main className=""></main>;
}
Answered by @ts-ignore
you forgot dependency array in useEffect
View full answer

2 Replies