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

97
Views
Pass a parameter through onclick in react with condition

I am trying to make an onClick with a condition but have an issue when trying to pass with a parameter.

The reason I want to do this is because inside the TableRow I have button that is only visible on desktop. But when people are on 900px and below I want to make it possible to click the entire row. The IsDesktop returns true or false depending on wether the user is over or under 900px

<TableRow hover key={site.id} onClick={isDesktop&&(e)=>handleSubmit(e,site.url)}>
  <TableRowSite site={site} index={index} />
</TableRow>

This is what I have tried so far. But it keeps giving me an error.

Parsing error: Unexpected token, expected "}" (31:67)

I have seen several tutorials using a condition in the onClick but never with a parameter but I believe it is possible as it makes sense.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I would try to do something like this:

<TableRow hover key={site.id} onClick={isDesktop ? (e) => handleSubmit(e, site.url) : undefined}>

Otherwise, you're simply trying to pass a Boolean value to an event handling prop that's expecting a callback which is seemingly what the issue at hand is caused by.

about 4 years ago · Santiago Trujillo Report

0

A quick fix you your component looks like

<TableRow hover key={site.id} onClick={isDesktop&&((e)=>handleSubmit(e,site.url))}>
    <TableRowSite site={site} index={index} />
</TableRow>

Simply wrap everything after the && in parentheses. That said, there's probably a better way to tackle this by rendering a desktop version or mobile version depending on the screen size of the user

about 4 years ago · Santiago Trujillo Report

0

Conditionally return an undefined handler

<TableRow
  hover
  key={site.id}
  onClick={isDesktop ? (e) => handleSubmit(e, site.url) : undefined}
>
  <TableRowSite {...{site, index }} />
</TableRow>

Or conditionally invoke the handleSubmit callback

<TableRow
  hover
  key={site.id}
  onClick={(e) => isDesktop && handleSubmit(e, site.url)}
>
  <TableRowSite {...{site, index }} />
</TableRow>
about 4 years ago · Santiago Trujillo 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!