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

157
Visualizações
React-select value not resetting to placeholder text on setState

I have two react select dropdown and I want the second dropdown to reset and show the placeholder text. I'm setting the stet variable to null when the first dropdown changes. I'm getting the value as null in the backend code, but the value on the ui is not removed. Can some on please help me on how to reset the value in second dropdown and show the placeholder text.

I have some call back functions which will set the options for the two dropdowns. So, please ignore those for now.

import React, {Component} from 'react'
Import Select from 'react-select'

Class test extends Component{
    constructor(props){
        super(props)
        this.state={
            val1= null,
            val1_options=[],
            val2= null,
            val2_options=[]
        }
        this.handleVal1Change = this.handleVal1Change.bind(this)
        this.handleVal2Change = this.handleVal2Change.bind(this)       
      }
    }
    handleVal1Change(value1) {
    
        this.setState({
            val1: value1.value,
            val2: null,
            val2_options: null
        })
    }
    
    handleVal2Change(value2) {
        this.setState({
            val2: value2.value
        })
    }
    
    render() {
        return (
            <div>
                <Select
                    placeholder='select val1'
                    options={this.state.val1_options}
                    onChange={this.handleVal1Change}
                />
            </div>
            <div>
                <Select
                    placeholder='select val2'
                    defaultValue={this.state.val2}
                    options={this.state.val2_options}
                    onChange={this.handleVal2Change}
                />
            </div>
        
        )
    }
    
export default test
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are using the Select components in uncontrolled mode, which means that the components are not connected to this.state.val1 or this.state.val2. The defaultValue you set for the second component is just used as an initial value. When this.state.val2 is changed in the first component, the second component will not notice the change.

Use value instead of defaultValue:

<Select
  placeholder='select val2'
  value={this.state.val2}
  options={this.state.val2_options}
  onChange={this.handleVal2Change}
/>
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