-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Select] Support defaultValue #11612
Comments
@engmagdy87 Maybe we should better warn about it. The Select component doesn't support the |
@oliviertassinari If it's a controlled component, how can we programmatically select a default value for the user? Currently, I'm using |
@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; |
Thanks for the reply! Your example is actually similar to what I am doing, except it's using the new React Hooks. In my case, since I want the value to change based on props, I would use the
I do admit as a novice React dev, this issue did give me plenty of grief and burned too much of my time. Especially considering that most of the other components do have a defaultValue param. What is involved in implementing this? Would it be worth it to at least update the doc to show an example of how to set a default value based on props? I could contribute that much at the least. |
This is a generic concern. I would challenge the need in the first place. Maybe you can simplify the situation by changing the requirements. |
This comment has been minimized.
This comment has been minimized.
My bad, it's already supported. |
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:
Context
Your Environment
The text was updated successfully, but these errors were encountered: