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

117
Visualizações
How do I send a function through props in react typescript

Hei! I'm trying to have a button component to make 2 separate things : One is called "START" and I want to start a countdown from my StoreTimer.start() ... And the other one is "RESET" where I want to reset my countdown from my StoreTimer.reset(). The problem is that I don't know how to do it . Let's take for example my code:

<StartButton name="Start"
              onClick={()=>{StoreTimer.start()}}/>
              <StartButton name="Reset"
              onClick={()=>{StoreTimer.reset()}}/>
            </div>
          </div>

here is my Home component where I imported StartButton , I want that button to have 2 different option to be reusable .... for example to start my countdown and reset it ... depends on the function that I pass...

here is my component

export interface Props {
  name?: string;
  color?: string;
  onClick?: () => void;
}

export const StartButton: React.FC<Props> = ({ name, color,onClick }) => {
  return (
    <>
      <div className="Buttons">
        <button
          className={"myButton"}
          style={{ backgroundColor: color }}
          onClick={() => onClick}
        >
          {name}
        </button>
      </div>
    </>
  );
};
export default StartButton;

If I make a separated component for start and reset and I don't pass the function through props it work... but I want to reuse the component StartButton and do 2 different thighs : Reset and Start . How I can do that? Please help me understand.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try adding () to your props function,and change the name onClick it is confusing :)

<button className={"myButton"}
        style={{ backgroundColor: color }}
        onClick={() => onClick()}
        >
        {name}
</button>

or like this :

<button className={"myButton"}
        style={{ backgroundColor: color }}
        onClick={onClick}
        >
        {name}
</button>
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