Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
ReactJS component set state not doing anything?

I'm a newbie in ReactJS. I'm trying to do a searchbar as following:

class SearchBar extends Component {
  constructor(props) {
    super(props);

  this.state = {term: ''};
  }

  render() {
    return (
      <div>
        <input onChange={ event => this.setState({ term: event.target.value })} />
      </div>
    );
  }
}

It works but the problem is I couldn't get the changed value of the input. What's wrong with it? I couldn't find a solution. Thanks for your help.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You are not setting the value of the input. You need to change your input to controlled component which has its value set by state so it's value only changes when the state changes.

<input
      value = { this.state.term }
      onChange= { (event) => this.setState({ term: event.target.value }) } />

Thus, this.state causes the component to render and when it re-renders the value of the element(input-in example) is set to the new value of this.state.term.

over 4 years ago · Santiago Trujillo Denunciar

0

You're not assigning a value to the input.

<input value={this.state.term} onChange={ event => this.setState({ term: event.target.value })} />
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda