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

196
Views
onClick function is not working as expected in React component

So i have this component ExpenseItem. Im trying to use onClick on the sub component Card. First i tried putting an arrow function inside the brackets, but it didn't work. Then i initialized the arrow function outside the return and tried to put the function inside the onClick brackets, but im not sure why its still not working. Here's the code:

import React from 'react';
import '../styles/ExpenseItem.css';
import ExpenseDate from './ExpenseDate';
import Card from '../Card.jsx';

const ExpenseItem = (props) => {
  
    const clickHandler = () => alert('im working');

    return (

        <Card className='expense-item' onClick={clickHandler}>
            <div>
                <ExpenseDate date={props.date} />
            </div>
            <div className='expense-item__description'>
                <div className='expense-item__descbox'>
                    <h2>{props.title}</h2>
                    <span>{props.type}</span>
                </div>
                <div className='expense-item__price'>{props.amount}</div>
            </div>
        </Card>
    )
};

export default ExpenseItem;


Thanks.

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

0

You also need to implement onClick on your Card component.

you can do something like below to your Card:

const Card = (props) => {
  return (
    <div className="card" {...props}>{props.children}</div>
  );
}

OR

const Card = (props) => {
  return (
    <div className="card" onClick={props.onClick}>{props.children}</div>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

do that in your Card component :

    const Card = (props) => {
     const classes = card ${props.className};
     return <div className={classes} onClick={props.onClick}>{props.children}</div> 
};

   export default Card;
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!