Hello, EveryOne
I’m Learning ReactJs.
I’m getting this error whenever I upadate isOpen state.
error : Too many re-renders. React limits the number of renders to prevent an infinite loop.
here is code
import React, { useState } from "react";
import { slide as Menu } from "react-burger-menu";
import { Link } from "react-router-dom";
import { useLocation } from "react-router-dom";
import "./SideBar.css";
const withLocation = (SideBar) => (props) => {
const location = useLocation();
return true ? <SideBar {...props} location={location} /> : "";
};
const SideBar = () => {
const [isOpen, setIsOpen] = useState(true);
const location = useLocation();
const homeClass = location.pathname === "/" ? "active-item" : "";
const aboutClass = location.pathname === "/about" ? "active-item" : "";
const projectClass = location.pathname === "/projects" ? "active-item" : "";
const skillsClass = location.pathname === "/skills" ? "active-item" : "";
const contactClass = location.pathname === "/contact" ? "active-item" : "";
return (
<Menu isOpen={isOpen}>
<Link
to='/'
onClick={setIsOpen(false)}
className={`menu-item ${homeClass}`}>
Home
</Link>
<Link to='/about' className={`menu-item ${aboutClass}`}>
About
</Link>
<Link to='/projects' className={`menu-item ${projectClass}`}>
Projects
</Link>
<Link to='/skills' className={`menu-item ${skillsClass}`}>
Skills
</Link>
<Link to='/contact' className={`menu-item ${contactClass}`}>
Contact
</Link>
</Menu>
);
};
export default withLocation(SideBar);
I don’t know how to solve this Problem.
4
Answers
onClick={setIsOpen(false)}
will be calling everytime you render your page. Setting a state will render your page too. What that mean is you are creating infinite loop.will solve your problem.
The problem is your button that fires the setState immediately causing too many re-renders,
Replace
To
if you want to read about more solutions check out this article
change your code on
Link
aswhen we call a function directly without arrow function, it calls automatically when it renders first time. and are using
setState
in in that function so component re-renders after executing first time and then again and again because of state update.The problem is with this line:
When setting attributes like this the code inside the brackets is executed during rendering and the value of the attribute is set to the result.
When calling
setIsOpen
the state is updated and this triggers a new render, which in turn callsesetIsOpen
again and triggers another render etc.What you want is the set the onClick handler to the function that should run when the link is clicked, to do that you should do this:
This will not call
setIsOpen
untill the link is clicked. As executing the code inside the brackets will not actually callsetIsOpen
but instead returns a function that will callsetIsOpen
when the function is called.Setting handlers can be a bit confusing at first, but remember that when setting handlers like this you want to set the value to a function and not the result of calling the function.