Bug: defaultValue property missing on select element ref
See original GitHub issueReact version: 18.2.0
Steps To Reproduce
- Set a
defaultValue
on a<select />
element. - After the element mounts, try to access that
defaultValue
through a ref of the element.
TypeScript is also missing the definition for defaultValue
on HTMLSelectElement
type
Link to code example: https://codesandbox.io/s/elated-orla-0p3dw7?file=/src/App.js
The current behavior
defaultValue
always returns undefined
The expected behavior
defaultValue
should return the defined value
Issue Analytics
- State:
- Created a year ago
- Comments:5
Top Results From Across the Web
React input defaultValue doesn't update with state
Personally I like to just use defaultValue if I'm just initializing it and then just use refs to get the value when I...
Read more >How to set default value in select using ReactJS
You can use an attribute defaultValue to set the default value in the Select menu If none option is integrated with this attribute...
Read more ><img>: The Image Embed element - HTML - MDN Web Docs
The HTML element embeds an image into the document. ... current source size to select one of the sources supplied by the srcset...
Read more >Tag Helpers in forms in ASP.NET Core - Microsoft Learn
The asp-route Tag Helper attribute can also generate markup for the HTML ... (are you missing a using directive or an assembly reference?)...
Read more >FormControl - Angular
Properties link ; defaultValue: TValue, Read-Only. The default value of this FormControl, used whenever the control is reset without an explicit value. See ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
React advises you agains using
selected
as React prop, nothing more. It is unreasonable to expect it to actually add a nonexistent property to a DOM node. If you want to work with DOM directly it is up to you to handle differences like thisI know I can read
selected
property, but React strongly advises against using it in the first place.I’m trying to create a reusable hook for uncontrolled forms (similar to React Hook Form) and I’m comparing the current value with the default value in order to check whether an input has been changed. This comparison can be done anywhere inside the form context, which may have several components nested and diverging trees.
My solution is to simply keep a reference to the fields registered in the form and access the DOM when I need information. That way I can freely change inputs without any unnecessary re-renders.
I can try to find a way around this, sure, but it just seems inconsistent that I’m advised against the
selected
property in favor ofdefaultValue
property, but that one is not provided anywhere in the DOM just asselected
property is.