Skip to content

Commit

Permalink
fix: 馃悰 fix react infinite loop error (#169)
Browse files Browse the repository at this point in the history
Setting state in the body of the component will cause below error:
> Error
Too many re-renders. React limits the number of renders to prevent an infinite loop.

So to change name for once, it has to be run inside of useEffect
  • Loading branch information
SeyyedKhandon committed Jul 15, 2023
1 parent 434ad82 commit 04453e6
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions content/1-reactivity/2-update-state/react/Name.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState } from "react";
import { useEffect, useState } from "react";

export default function Name() {
const [name, setName] = useState("John");
setName("Jane");

useEffect(() => setName("Jane"), []);

return <h1>Hello {name}</h1>;
}
}

0 comments on commit 04453e6

Please sign in to comment.