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

161
Vistas
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 Respuestas
Responde la pregunta

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