Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

401
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda