question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: defaultValue property missing on select element ref

See original GitHub issue

React version: 18.2.0

Steps To Reproduce

  1. Set a defaultValue on a <select /> element.
  2. 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

defaultValueshould return the defined value

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkurchatkincommented, Sep 19, 2022

I know I can read selected property, but React strongly advises against using it in the first place.

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 this

0reactions
antonio-costacommented, Sep 19, 2022

I 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 of defaultValue property, but that one is not provided anywhere in the DOM just as selected property is.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found