0

Im using react and tailwind. I trying to display a state based on clicking a nav button or hovering on the same button. So i am trying to have a display with the potential to show 1 of 3 elements. You can select the elements from the nav bar. I want to have it so that if you hover over the nav link, you get the element display, but if you move the mouse away it goes back to the selected element. It should display overview initially and then i can click on the links to swap element, or hover to get a display of the element.

The issue is when i add an onclick listener to my links. I get an infinite rerendering error.

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

How can i have a hover and click function

 const [displayOverview, setDisplayOverview] = useState(false);
 const [displayProjects, setDisplayProjects] = useState(false);
 const [displayDesigns, setDisplayDesigns] = useState(false);
 const [selectedNavElement, setSelectedNavElement] = useState('overview');
 const [displayFixed, setDisplayFixed] = useState(true)

 <ul className='flex lg:text-left gap-24'>
              <li
                onMouseEnter={() => {setDisplayOverview(true); setDisplayFixed(false)}}
                onMouseLeave={() => {setDisplayOverview(false); setDisplayFixed(true)}}
              >
                Overview
              </li>
              <li
                onMouseEnter={() => {setDisplayDesigns(true); setDisplayFixed(false)}}
                onMouseLeave={() => {setDisplayDesigns(false); setDisplayFixed(true)}}
              >
                Designs
              </li>

              <li
                onMouseEnter={() => {setDisplayProjects(true); setDisplayFixed(false)}}
                onMouseLeave={() => {setDisplayProjects(false); setDisplayFixed(true)}}
              >
                <span onClick={setSelectedNavElement('projects')}>Projects</span>
              </li>
            </ul>
          </nav>
          {/* Content */}
          <section className='bg-red-500'>
            {displayOverview && <Overview />}
            {displayDesigns && <Designs />}
            {displayProjects && <Projects />}
            {(selectedNavElement === 'overview' && displayFixed === true) && <Overview />}
            {(selectedNavElement === 'designs' && displayFixed === true) && <Designs />}
            {(selectedNavElement === 'projects' && displayFixed === true) && <Projects />}
          
          </section>
  • onMouseover (while it is present, not only momentary on occurence) and onMouseout is the normal name not onMouseLeave – Samuel Marchant Mar 13 '23 at 03:31
  • ALTHOUGH I have noticed you have not placed quotes or double quotes around your html listener(tag attribute value) javascript! Always enclose them in at least single quotes. E.G. onClick='{setSelectedNavElement("projects")}'>Projects. and use double quotes inside the listener single quotes attribute value. Try this syntax system first. – Samuel Marchant Mar 13 '23 at 03:46

1 Answers1

0

Missing arrow function

<span onClick={setSelectedNavElement('projects')}>Projects</span>

to

<span onClick={() => setSelectedNavElement('projects')}>Projects</span>
dot_cr2
  • 69
  • 1
  • 5