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

406
Views
calling multiple function when onClick the Icon

Am trying to call two different function when the user onClick the Icon but am getting Error: Expected onClick listener to be a function, instead got a value of object type. please can someone to know what am doing wrong here.

Here's my code

function fullComponent (){
 const dispatch = useDispatch();

 const addItemToBasket = () => {
    dispatch(
      addToBasket({
        id,
        name,
        price,
       })
    );
    
  };
  const TodoComponent = {
    backgroundColor: "#44014C",
   }
 return (
   <div> <StarBorderIcon onClick={addItemToBasket &&TodoComponent}<div />
 )
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Firsts and foremost, TodoComponent is not a function which makes your onClick a bit non-sensical.

One thing you can do if you want to invoke multiple functions with one onClick call is to create a onClickHandler function which bundles your function calls together, roughly speaking.

const function1 = () => {
 ...do something
}

const function2 = () => {
 ...do something else
}

const onClickHandler = () => {
 function1()
 function2()
}

<div><StarBorderIcon onClick={onClickHandler}<div />
about 4 years ago · Juan Pablo Isaza Report

0

You can use multiple functions by passing an anonymous function in onClick event as shown below

function fullComponent (){
     const dispatch = useDispatch();
    
     const addItemToBasket = () => {
        dispatch(
          addToBasket({
            id,
            name,
            price,
           })
        );
        
      };
      const TodoComponent = {
        backgroundColor: "#44014C",
       }
     return (
       <div> <StarBorderIcon onClick={() => {
                            addItemToBasket();
                            TodoComponent();
                            }}
       <div />
     )
    }
about 4 years ago · Juan Pablo Isaza Report

0

What you need to understand is that value1 && value2 will end up being either value1 or value2.

It will not hold both two values.

So if you come back to your original issue with addItemToBasket && TodoComponent, it will be just TodoComponent.

So complier is failing because TodoComponent is not an event handler.

If we imagine TodoComponent is a function, we can fix it like the following.

const onClickHandler = () => {
     addItemToBasket();
     TodoComponent()
}

<div><StarBorderIcon onClick={onClickHandler}<div />
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!