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

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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