Skip to content

Commit

Permalink
[Autocomplete] Warn when mixing uncontrolled and controlled (#19060)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw authored and oliviertassinari committed Jan 3, 2020
1 parent 1f5e98b commit 7f0be94
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@sjsingh85

sjsingh85 Jan 8, 2020

Contributor

@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.

This comment has been minimized.

Copy link
@m4theushw

m4theushw Jan 8, 2020

Author Member

Hi, both examples are using loose equality so if valueProp is undefined or null it will evaluate to true. Now it's doing a strict equality. These components and others are being refactored in #19070 to use an abstraction that provides the controlled/uncontrolled logic. This will make all components consistently.

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;
Expand Down

0 comments on commit 7f0be94

Please sign in to comment.