Home > Net >  HTML Select Input Tag bringing error with "selected" attribute in React JS
HTML Select Input Tag bringing error with "selected" attribute in React JS

Time:07-14

Seeing this warning in React Warning: Use the defaultValue or value props on instead of setting selected on . What does it implies?

CodePudding user response:

It's basically a warning that encourages you to change your code from doing <option selected> to doing <select defaultValue="theValue"> or using a controlled select.

It's probably because React wants to keep consistency between the Form components.

Edit: you can probably implement it this way using useState hook and a controlled select:

const [value, setValue] = useState("defaultValue");

...

<select value={value} onChange={(e) => {setValue(e.target.value)}}>
    <option value="defaultValue"> Default </option>
    <option value="otherValue"> Other </option>
</select> 
  • Related