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

177
Views
Llamar a funciones de nuevo onClick en React

Trabajo en React Project y tengo algunas funciones que definen algunas condiciones cuando renderizo los componentes en la página, pero necesito volver a llamar a todas las funciones en los componentes cuando hago clic en el botón y renderizo números aleatorios.

ejemplo:

 export default function App(){ func1(){ // some func //brings random number } func2(){ // some func //brings random number } func3(){ // some func //brings random number } const definedOne = func1(); const definedTwo = func2(); const defineTree = func3(); return( <div>{definedOne}<div> <div>{definedTwo}<div> <div>{definedTree}<div> <button onClick={// what need to click?}>Run Functions Again</button> ) }
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Su código para React está incompleto. Para que un botón funcione, necesita un código como este:

 <Button variant="outline-secondary" id="button-addon2" disabled={email === ''} onClick={() => sendReport()}> Send report </Button>

Ahora esto llamará a una función llamada sendReport(). Por ejemplo:

const sendReport = async () => { service.mailItem(email).catch((e) => {setError(e.message)}); }

Su código no tiene método onClick() .

about 4 years ago · Santiago Gelvez Report

0

Sus tres números deben moverse al estado , lo que le permite actualizarlos al hacer clic.

Use una función handleClick para actualizar las tres variables de estado.

 export default function App(){ func1(){ // some func //brings random number } func2(){ // some func //brings random number } func3(){ // some func //brings random number } handleClick(){ setDefinedOne(func1()); setDefinedTwo(func2()); setDefinedThree(func3()); } const [definedOne, setDefinedOne] = useState(func1()); const [definedTwo, setDefinedTwo] = useState(func2()); const [definedThree, setDefinedThree] = useState(func3()); return( <div>{definedOne}<div> <div>{definedTwo}<div> <div>{definedTree}<div> <button onClick={handleClick}>Run Functions Again</button> ) }
about 4 years ago · Santiago Gelvez Report

0

La función onClick se puede usar así:

onClick = {handleClickFn}

o

onClick = {(e) => handleClickFn(e)}

Ambos funcionan bien.

 export default function App(){ const handleClickFn = (e) => { console.log("You clicked button"); func1(); func2(); func3(); } func1(){ // some func //brings random number } func2(){ // some func //brings random number } func3(){ // some func //brings random number } const definedOne = func1(); const definedTwo = func2(); const defineTree = func3(); return( <div>{definedOne}<div> <div>{definedTwo}<div> <div>{definedTree}<div> <button onClick={handleClickFn}>Run Functions Again</button> ) }
about 4 years ago · Santiago Gelvez 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!