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

148
Visualizações
Select value doesn't change the first time I trigger onChange event

I have select menus and i am trying to change a value when i select an option using optionSelect method. When i first select an option, the value is not being updated in the console.log. It only changes from the second time. Not sure what I am doing wrong. Do i need to add something in the componentDidMount!

constructor(props) {
    super(props);
    this.state = {
        value : '',
    };
}

optionSelect(option){
    this.setState({value : option.target.value});
    console.log(this.state.value)
}

render() {
    return (
        <React.Fragment>
            <Select name="item"
                required={this.props.required ? 'required' : ''}
                defaultValue={this.state.value}
                onChange={e => this.optionSelect(e)}
            >
                <option disabled value="">{this.props.placeholder}</option>
                {
                    this.props.optionData && this.props.optionData.length ?
                    this.props.optionData.map(option => (
                        <option key={option.id} value={option.id}>
                            {option.name}
                        </option>
                    )) : null
                }                   
            </Select>
        </React.Fragment>
    );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

setState() in React is not synchronous. While you try to console.log the value. The value has not updated yet. If you want to console.log a value immediately after it changes, add a

componentDidUpdate(){
 console.log('Value', this.state.value)
} 
about 4 years ago · Juan Pablo Isaza 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