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

324
Views
ReactJs ¿Cómo cambiar un ícono de un botón en el evento de clic?

Estoy usando IconButton de material-ui y quiero cambiar el ícono del botón después del evento de clic/toque.

 var tableModeElement = <IconButton key="tableModeButton" onTouchTap={() => { this.setState(prevState => ( { isCardView: !prevState.isCardView })) } }> <i className="material-icons theme-color-p1"> {this.state.isCardView ? "view_module" : "list"} </i> </IconButton>

La expresión {this.state.isCardView ? "view_module" : "list"} solo se evalúa una vez y no más después de eso. Pensé que si cambio el estado, ¿forzaré un nuevo procesamiento? ¿Qué estoy haciendo mal?

EDITAR: agregó que el iconButton está asignado a una variable. Si incluyo el <IconButton> directamente en el método de procesamiento, funciona bien. Tuve que reasignar la variable para que funcionara.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Creo que esto funcionaría:

 class IconButton extends React.Component { constructor(props) { super(props); this.state = { isCardView: false, } } render() { return ( <a className="btn btn-primary" onClick={()=>this.setState({ isCardView: !this.state.isCardView })}> { this.state.isCardView ? <span className="glyphicon glyphicon-remove-sign" /> : <span className="glyphicon glyphicon-ok-sign" /> } &nbsp;&nbsp;BUTTON </a> ); } } class App extends React.Component { render () { return ( <div> <IconButton /> </div> ); } } ReactDOM.render(<App/>,document.getElementById('root'));

Estoy usando bootstrap, pero cualquier sistema de íconos funcionaría

http://codepen.io/cjke/pen/MJvXNo?editors=0010

over 4 years ago · Santiago Trujillo Report

0

Establecer el texto "view_module" o "list" en un elemento <i> no cambiará el icono del botón

Debe tener un icono anidado dentro del botón, por ejemplo:

 <IconButton key="tableModeButton" onTouchTap={() => { this.setState({ isCardView: !this.state.isCardView }) }}> {this.state.isCardView ? <IconA /> : <IconB /> } </IconButton>
over 4 years ago · Santiago Trujillo Report

0

Esto es lo mejor que se me ocurrió:

 <IconButton key="tableModeButton" onTouchTap={(e) => { let show = this.state.prevState.isCardView; let index = show.indexOf('show'); if (index != -1) { show = 'hide'; } else { show = 'show'; } this.setState(prevState => ({ isCardView: show })); event.preventDefault() } }>
over 4 years ago · Santiago Trujillo 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!