-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Autocomplete] Warn when mixing uncontrolled and controlled (#19060)
- Loading branch information
1 parent
1f5e98b
commit 7f0be94
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,6 +192,25 @@ export default function useAutocomplete(props) { | |
}); | ||
const value = isControlled ? valueProp : valueState; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
React.useEffect(() => { | ||
if (isControlled !== (valueProp !== undefined)) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
m4theushw
Author
Member
|
||
console.error( | ||
[ | ||
`Material-UI: A component is changing ${ | ||
isControlled ? 'a ' : 'an un' | ||
}controlled useAutocomplete to be ${isControlled ? 'un' : ''}controlled.`, | ||
'Elements should not switch from uncontrolled to controlled (or vice versa).', | ||
'Decide between using a controlled or uncontrolled useAutocomplete ' + | ||
'element for the lifetime of the component.', | ||
'More info: https://fb.me/react-controlled-components', | ||
].join('\n'), | ||
); | ||
} | ||
}, [valueProp, isControlled]); | ||
} | ||
|
||
const { current: isInputValueControlled } = React.useRef(inputValueProp != null); | ||
const [inputValueState, setInputValue] = React.useState(''); | ||
const inputValue = isInputValueControlled ? inputValueProp : inputValueState; | ||
|
@m4theushw @oliviertassinari Hey folks, checking for valueProp to not be undefined here and to be not null here and here I think it should be checked with not null here - consistent one with other controls.