0

I tried the solution below, in theory it works, but react returns this: Warning: Use the defaultValue or value props on instead of setting selected on .

<Select>
   <OptionPlaceholder selected disabled>
         Escolha uma opção...
   </OptionPlaceholder>
</Select>

The purpose is only to remove this warning, but any other solution that ive tryied dont get me the expecting result

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • 1
    Possible duplicate of [Conditionally disabled select option in React](https://stackoverflow.com/questions/51164838/conditionally-disabled-select-option-in-react) – Mike Poole Nov 11 '19 at 18:37

3 Answers3

0

you missed a props

<option value="" disabled={this.props.defaultDisabled}>

Mileta Dulovic
  • 1,036
  • 1
  • 14
  • 33
0

You want to set the value or defaultValue attribute of the select in React instead of the selected prop of the option!

Here is an example with the value attribute:

import React, { Component } from 'react'

// ...

export default class App extends Component {
  state = {
    value: '',
  }

  handleChange = event => {
    const { value } = event.target

    this.setState({ value })
  }

  render () {
    const { value } = this.state

    return (
      <Select value={value} onChange={this.handleChange}>
        <OptionPlaceholder disabled>
          Escolha uma opção...
        </OptionPlaceholder>
      </Select>
    )
  }
}

Or take a look here

Vl4dimyr
  • 876
  • 9
  • 27
0

It's just the reference code, because you need to add an option as defaultValue and handle it during your validation if it is selected or not. Or make use of onChange handlers to modify a state variable:

class App extends React.Component {

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

    render(){
       return(
           <select value={this.state.value} onChange={(e)=>{this.setState({value: e.target.value})}}>
              <option value='1' disabled>Select</option>
              {
                  [2,3,4].map((i,j)=>{
                      return <option key={i} value={i}>{i}</option>
                  })
              }
           </select>
       );
    }
}
tripleee
  • 175,061
  • 34
  • 275
  • 318
Aravind
  • 129
  • 1