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

169
Views
Activar la función onClick desde dentro del contenido condicional

Estoy usando el siguiente código. Dentro del componente ContentTwo, hay un botón, quiero que pueda activar la misma función que el botón en el renderizado, cuando es el contenido en vivo. ¿Cómo haría esto?

 class Toggle extends React.Component { constructor(props) { super(props); this.state = {isToggleOn: true}; // This binding is necessary to make `this` work in the callback this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState(prevState => ({ isToggleOn: !prevState.isToggleOn })); } render() { return ( <div > {this.state.isToggleOn ? <ContentOne /> : <ContentTwo />} <button onClick={this.handleClick}>Swap</button> </div> ); } } function ContentOne(props) { return(<h1>Content One</h1> ); } function ContentTwo(props) { return(<button onClick={this.handleClick}>Swap like the other button</button> ); } ReactDOM.render( <Toggle />, document.getElementById('root') );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede pasar this.handleClick a ContentTwo y acceder a él desde los accesorios:

 <ContentTwo handleClick={this.handleCLick} />

y entonces

 function ContentTwo(props) { return(<button onClick={props.handleClick}>Swap like the other button</button> );
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!