elements? [edit] This was due to my own confusion, apologies. Setting prope...">

Why does React say not to set the "selected" property on <option> elements?

[edit] This was due to my own confusion, apologies.

Setting property selectedon elements <option>in JSX works fine, but causes React throw throw:

Warning: Use the 'defaultValue' or 'value' props on <select> instead of setting 'selected' on <option>.

The setting defaultValuein the parent element <select>invokes the default value valuefor <select>this parameter, but does not set the default selected <option>. Thus, it throws what the user sees, and what is actually selected from synchronization.

Setting the property valueon the parent element <select>then forces me to add an onChange handler, set the value to the state of the component, and write a bunch of additional code to accomplish what just setting selectedto Elements <option>has one word.

Does anyone know why React issues this warning? I do not want to write a bunch of additional code to remove the warning, which does not seem to be true. It works great, as far as I can tell, so why shouldn't I use it?

+4
source share
1 answer

"", : https://facebook.imtqy.com/react/docs/forms.html

, , <SELECT> "". onChange, , , .

<OPTION>, , , . , , A . B. B , , A , . onClick B A B Select, A.

React "" <SELECT>, , , , ( , React .

+4

All Articles