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.

[Select] Support defaultValue

See original GitHub issue
  • [x ] This is a v1.x issue (v0.x is no longer maintained).
  • [x ] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

It’s a form used for adding record or edit this record., so when add new record defaultValue has no value but when editing this record, defaultValue will be the value which may be edited.

If I remove value prop, error appear that it’s required prop

Current Behavior

I got this error:

SelectInput contains an input of type hidden with both value and defaultValue props. Input elements must be either controlled or uncontrolled (specify either the value prop, or the defaultValue prop, but not both). Decide between using a controlled or uncontrolled input element and remove one of these props

Context

Your Environment

Tech Version
Material-UI v1.0.0
React v16.4.0
browser Chrome v68.0.3440.5
etc

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
oliviertassinaricommented, Oct 6, 2019

@engmagdy87 Maybe we should better warn about it. The Select component doesn’t support the defaultValue prop as we are speaking. The component needs to be controlled but I’m not against implementing this behavior.

5reactions
oliviertassinaricommented, Jan 2, 2019

@atifmansoor I don’t think that performance is a concern here. It’s more about code correctness and how easy it’s for somebody to get it right. Regarding implementing the default value behavior. It should be as easy as:

function ControlledSelect() {
  const [age, setAge] = React.useState(10);

  function handleChange(event) {
    setAge(event.target.value);
  }

  return (
    <Select
      value={age}
      onChange={handleChange}
      inputProps={{
        name: 'age',
        id: 'demo-controlled-open-select',
      }}
    >
      <MenuItem value="">
        <em>None</em>
      </MenuItem>
      <MenuItem value={10}>Ten</MenuItem>
      <MenuItem value={20}>Twenty</MenuItem>
      <MenuItem value={30}>Thirty</MenuItem>
    </Select>
  );
}

export default ControlledSelect;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: Use the 'defaultValue' or 'value' props on <select ...
I am using selected attribute on option but React generates a warning asking me to use default value on select. For e.g. render:...
Read more >
[Select] Support defaultValue · Issue #11612 · mui/material-ui
The Select component doesn't support the defaultValue prop as we are speaking. The component needs to be controlled but I'm not against ...
Read more >
How to set the default value for an HTML <select> element
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean...
Read more >
DefaultValue Property - Microsoft Support
Specifies a String value that is automatically entered in a field when a new record is created. For example, in an Addresses table...
Read more >
Default-Value via GET-Parameter in Select-Fields
Is there a way to also pre-populate dropdown menus using a query-string-parameter? Many thanks in advance for your help and have a nice...
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