Next.js Discord

Discord Forum

Unable to understand Error Warning: Cannot update a component (`HotReload`) while rendering

Answered
Sporting Lucas Terrier posted this in #help-forum
Open in Discord
Sporting Lucas TerrierOP
Trying to build a simple portfolio website for myself, and I keep seeing this error in the console
Warning: Cannot update a component (`HotReload`) while rendering a different component (`ProjectCards`). To locate the bad setState() call inside `ProjectCards`, follow the stack trace as described in

I have my home page under home/page.tsx and a simple Cards component under components/ui/project_cards.tsx. Project_cards simply fetches data from a json file using UseState to show an array of cards and I am calling it in my home page as
<Cards>
  <Project_cards/>
</Cards>

Project Cards -
function ProjectCards() {
  const [projects, setProjects] = useState<project_data | any>({});
  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await fetch("/project_content.json", {
          method: "GET",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
          },
        });
        const data = await response.json();
        console.log(data);
        setProjects(data);
      } catch (error) {
        console.error("Error fetching project data:", error);
      }
    };
    fetchData();
  }, [setProjects]);
  console.log(projects);
  console.log(typeof projects);
return (
    <Stack spacing={4} direction={["column", "row"]} wrap="wrap">
      {projects.map((project: project_data) => (
        <Card key={project.id} className="rounded-lg border-4">
          <Image
            src="https://bit.ly/2k1H1t6"
            alt="Project Image"
            objectFit="cover"
            height="200px"
          />
          <CardHeader>
            <Heading className="text-2xl text-white">{project.name}</Heading>
          </CardHeader>
          <CardBody>
            <Text>{project.overview}</Text>
          </CardBody>
          <CardFooter>
            <Button
              colorScheme="blue"
              mr={2}
              onClick={() => window.open(project.demo)}
            >
              Demo
            </Button>
            <Button
              colorScheme="green"
              onClick={() => window.open(project.github)}
            >
              Github
            </Button>
          </CardFooter>
        </Card>
      ))}
    </Stack>
  );
}

export default ProjectCards;

I just dont understand this error and whats causing this, any info is appreaciated.
Answered by Ray
oh you could just import it if its a json file
View full answer

36 Replies

@Sporting Lucas Terrier Trying to build a simple portfolio website for myself, and I keep seeing this error in the console Warning: Cannot update a component (`HotReload`) while rendering a different component (`ProjectCards`). To locate the bad setState() call inside `ProjectCards`, follow the stack trace as described in I have my home page under home/page.tsx and a simple Cards component under components/ui/project_cards.tsx. Project_cards simply fetches data from a json file using **UseState** to show an array of cards and I am calling it in my home page as <Cards> <Project_cards/> </Cards> Project Cards - function ProjectCards() { const [projects, setProjects] = useState<project_data | any>({}); useEffect(() => { const fetchData = async () => { try { const response = await fetch("/project_content.json", { method: "GET", headers: { Accept: "application/json", "Content-Type": "application/json", }, }); const data = await response.json(); console.log(data); setProjects(data); } catch (error) { console.error("Error fetching project data:", error); } }; fetchData(); }, [setProjects]); console.log(projects); console.log(typeof projects); return ( <Stack spacing={4} direction={["column", "row"]} wrap="wrap"> {projects.map((project: project_data) => ( <Card key={project.id} className="rounded-lg border-4"> <Image src="https://bit.ly/2k1H1t6" alt="Project Image" objectFit="cover" height="200px" /> <CardHeader> <Heading className="text-2xl text-white">{project.name}</Heading> </CardHeader> <CardBody> <Text>{project.overview}</Text> </CardBody> <CardFooter> <Button colorScheme="blue" mr={2} onClick={() => window.open(project.demo)} > Demo </Button> <Button colorScheme="green" onClick={() => window.open(project.github)} > Github </Button> </CardFooter> </Card> ))} </Stack> ); } export default ProjectCards; I just dont understand this error and whats causing this, any info is appreaciated.
could you show the code on home/page.tsx also?
@Ray could you show the code on `home/page.tsx` also?
Sporting Lucas TerrierOP
sure, give me a second
The file is kinda big but the error is I believe coming from line 68
@Sporting Lucas Terrier The file is kinda big but the error is I believe coming from line 68
maybe try comment out AnmiationOnScroll
 //<AnimationOnScroll>
              <ProjectCards />
 //</AnimationOnScroll>
Sporting Lucas TerrierOP
its throwing me an error despite of AnimationOnScroll
@Sporting Lucas Terrier its throwing me an error despite of AnimationOnScroll
can't render the page without it?
@Ray can't render the page without it?
Sporting Lucas TerrierOP
nope, I commented out AnimationOnScroll and it was still giving me an error on Project_cards, if i comment out Project_Cards then I can render the home page
Sporting Lucas TerrierOP
yes, I think something is wrong with my fetch request(?)
Sporting Lucas TerrierOP
I do see the data logged
console.log(data);
Sporting Lucas TerrierOP
its fetching as an object
and under this hot reload error its throwing me projects.map is not a function
it should be fetching the data from the json file as an array but its coming as an object
@Ray console.log(data);
Sporting Lucas TerrierOP
its not showing any data
sorry I meant its not showing me any data
what is /project_content.json?
Sporting Lucas TerrierOP
line 42: console.log(projects)
{
  "projects": [
    {
      "id": 2,
      "name": "Project 2",
      "overview": "This is the overview of Project 2.",
      "summary": "This is the summary of Project 2.",
      "demo": "https://example.com/project2",
      "github": ""
    },
    {
      "id": 3,
      "name": "Project 3",
      "overview": "This is the overview of Project 3.",
      "summary": "This is the summary of Project 3.",
      "demo": "https://example.com/project3",
      "github": ""
    },
    {
      "id": 4,
      "name": "Project 4",
      "overview": "This is the overview of Project 4.",
      "summary": "This is the summary of Project 4.",
      "demo": "https://example.com/project4",
      "github": ""
    }
  ]
}
just some sample placeholders in a json format
purpose of this is to render out each project in its own separate card instead of me hard coding this in tsx
oh you could just import it if its a json file
Answer
or you have to create a route handler and return the json
fetch(filePath) doesn't work on client
Sporting Lucas TerrierOP
ohhhhh
I did not know I could just import this as a raw json file
Sporting Lucas TerrierOP
then I dont need to use UseState?
Sporting Lucas TerrierOP
oh wow, I feel so stupid haha it works
I was making it complicated for no reason with UseEffect and fetching data
Thanks for your help
lol
np