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

121
Views
Execute onClick immediately without getting error

I have a modal in React JS where I want to execute a function upon opening the modal. This is a button function and it works completely fine so far by having it like so:

<button onClick={functionExample("stringParam", true)} id='buttonId' className="buttonClass" />

The only issue with this is that it throws this error:

index.js:1 Warning: Expected `onClick` listener to be a function, instead got a value of `object` type.

Is there any way to do this without getting the error?

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

0

You can call it with an arrow function.

onClick={() => functionExample("stringParam", true)}

about 4 years ago · Juan Pablo Isaza Report

0

You have to pass a function, which will be later called by React like this:

<button onClick={() => functionExample("stringParam", true)} id='buttonId' className="buttonClass" />

When you do JS "stores" the function for later execution, but if you write

onClick={functionExample("stringParam", true)}

your code gets execute in place, returns an object and this is what React complaining about.

about 4 years ago · Juan Pablo Isaza Report

0

Your onClick listener is a calling the method on render instead of onClick

   <button onClick={() => functionExample("stringParam", true)} id='buttonId' className="buttonClass" />

Change your onClick to

   onClick={() => functionExample("stringParam", true)}
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!