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

78
Visualizações
Perform 2 functions OnClick ReactJS

hope you are all good.

I have button onclick that call 2 functions BUT it is not working and i dont have any errors displaying in the console, so i don't know where the problem is.

Here is my code

  <div className="d-grid gap-2">
            <Bouton btnCss = 'btn btn-success my-3' 
             click= { () => { this.props.handleSubmit; this.setState({begin : true})}}>
              Commencer la tâche
             </Bouton>
            </div>
        </form>
       
            </>
        )
    }
}

export default withFormik({
    mapPropsToValues : () => ({
        tache : '',
        unite : '',
        commentaire : ''

    }),
    validationSchema : Yup.object().shape({
        tache : Yup.number()
        .required('Choisissez une tâche'),
        unite : Yup.number()
        .required('Choisissez une unite'),
        commentaire : Yup.string()
        .min(5, 'Le Texte est trop court')
        .required('Veuillez entre un texte')
    }), 
    handleSubmit : (values, {props, resetForm}) => {
        resetForm();
        props.validation(values.tache, values.unite, values.commentaire);
    }
})
(taskForm)

Thank you for your help mates !

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

0

Why don't you call one function, that calls the next two functions? Something like this:

handleClick() {
   function1();
   function2();
}


<Bouton btnCss = 'btn btn-success my-3' 
             onClick= { () => { this.handleClick() }}>
              Commencer la tâche
             </Bouton>

You can always add async/await if you need to wait for the 1st one to complete before you fire the 2nd one. 🙂

about 4 years ago · Juan Pablo Isaza Relatório

0

You are not calling your this.props.handleSubmit

It should be this.props.handleSubmit()

When you were previously calling your function you were using:

<Bouton click= {this.props.handleSubmit}>Commencer la tâche</Bouton>

You were in fact passing to your custom component a reference to the function that will be called when the button is clicked.

The button then calls the function. You could say the onClick function adds the ()

Like you would when you're getting a callback in your arguments:

const functionWithACallback = (callback) =>
{
   // Do stuff
   callback();
}

However now what you are doing is passing an anonymous function to be called when your button is clicked. You're passing:

() => {
    this.props.handleSubmit; 
    this.setState({begin : true})
}

However here, this.props.handleSubmit is never called.

Also comments made and the other answer are the good practices to follow namely:

  1. You should follow the react naming conventions and name your button click handler onClick instead of click.
  2. You should create a separate function named handleClick and call it when your button is clicked to keep your return statement as lean as possible.
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