Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!