Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
onClick event firing off after render in React

I have a sidebar menu in react, when buttons are clicked in this sidebar I want to update state of a variable. However the event fires off on render instead of waiting for a click, help much appreciated!

Side Bar

return (
        <>
        <div id="container1">
            <ProSidebar id='tester1'>
                <SidebarHeader>
                    <h1>Header Title</h1>
                    
                </SidebarHeader>

                <SidebarContent>
                    <Menu iconShape="square">
                              
                        <MenuItem onClick={funx(1)} icon={<FiHome className='sideBarIcon'/>}></MenuItem>
                        <MenuItem icon={<FaList className='sideBarIcon'/>}></MenuItem>
                        <MenuItem icon={<FaRegHeart className='sideBarIcon'/>}></MenuItem>
                        <MenuItem icon={<RiPencilLine className='sideBarIcon'/>}></MenuItem>
                        <MenuItem icon={<BiCog className='sideBarIcon'/>}></MenuItem>

                    </Menu>
                </SidebarContent>

                <SidebarFooter>
                    <Menu iconShape="square">
                        <MenuItem icon={<FiLogOut />}>{x}</MenuItem>
                     </Menu>
                </SidebarFooter>
            </ProSidebar>
        </div>
        </>
    );

And where the state gets set:

const [x, setX] = useState([]);

    const funx = (e) => {
        console.log(e);
        setX(e);
        
    }

I'd like to add if I remove the e parameter from funx, and only call funx in my menu, the onclick event seems to be working fine. However I want to pass 1/2/3/etc. depending on what's clicked

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

onClick={funx(1)} the code inside the brackets is evaluated at render, so you're actually calling the function. You want to pass a function reference. If you need to pass the 1 you could do:

 <MenuItem onClick={() => funx(1)} ...>
about 4 years ago · Juan Pablo Isaza Report

0

Instead of setting the onClick event handler function like <MenuItem onClick={funx(1)} icon={<FiHome className='sideBarIcon'/>}></MenuItem>

You have to change the as like code given below:

 <MenuItem onClick={() => funx(1)} icon={<FiHome className='sideBarIcon'/>}></MenuItem>

Or you can modify the event handler function as shown below:

const funx = (value) = (event) => {
    console.log(value);
    setX(value);
}

Now you can call the function as below:

<MenuItem onClick={funx(1)} icon={<FiHome className='sideBarIcon'/>}></MenuItem>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!