Next.js Discord

Discord Forum

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

Answered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hello every one I met an error message when trying to lunch the render on my localHost:

here is my code:
"use client";
import { useState } from "react";

const NavButton = () => {
    let [clickNav, setClickNav] = useState(false);
    const clickedOrNot = () => {
        setClickNav(!clickNav);
    }

    
    return <div className="navButton" onClick={clickedOrNot()}>
                { clickNav === false ?
                    <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368">
                        <path d="M120-240v-80h720v80H120Zm0-200v-80h720v80H120Zm0-200v-80h720v80H120Z"/>
                    </svg>
                    :
                    <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368">
                        <path d="m256-200-56-56 224-224-224-224 56-56 224 224 224-224 56 56-224 224 224 224-56 56-224-224-224 224Z"/>
                    </svg>
                }
            </div>
}

export default NavButton;


I am trying to change on the user click the svg of my nav bar to switch on click between a menu Icon and a cross Icon. The menu Icon when clicked open the nav bar and switch to a cross (to close the navbar when clicked) and the opposite

And... apparently Next encounter a rendering problem and signal the possibility of an infinity loop but I don't know why...

⨯ Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
at clickedOrNot (./components/NavButton.js:14:9)
at NavButton (./components/NavButton.js:18:18)
digest: "60856267"
GET / 500 in 206ms

Do you have any idea ?

Thanks in advance for your support 🙏
Answered by Sun bear
Try to change

onClick={clickedOrNot()} to onClick={clickedOrNot}
View full answer

15 Replies

Cape lionOP
first SVG on my ternary operator is the menu Icon and the second svg is the cross
Sun bear
Try to change

onClick={clickedOrNot()} to onClick={clickedOrNot}
Answer
Sun bear
And just for better readable code I would rename a few things like

[navOpen, setNavOpen] = ...

and clickedOrNot to toggleNav

and instead of let [clicked... better use const [clicked...
@Sun bear Try to change `onClick={clickedOrNot()}` to `onClick={clickedOrNot}`
Cape lionOP
thanks I will try it
It worked !!! you're a boss @Sun bear !
it's weird because I tried with a const before, instead a let and it put me an error message has with a const he can't change the value so I changed it
and I tried before to launch the rendering unputting the "()" to
`onClick={clickedOrNot()}
and it didn't worked
@Cape lion It worked !!! you're a boss <@908094633660268555> !
Sun bear
Haha happy to hear that.

You should use const for useState...

If you use const in a function like

const count = 1;
count++;


It wont work.
Cape lionOP
Yes I know it thanks
I hava another question if you can haha
Or I will open a new request
it's about client rendering
I met an issue with the shared state
I will open a new request
Cape lionOP