Skip to content

Commit

Permalink
[utils] Allow passing NaN as defaultValue to useControlled
Browse files Browse the repository at this point in the history
  • Loading branch information
iammminzzy committed Mar 19, 2024
1 parent 9585d28 commit 94adbb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/mui-utils/src/useControlled/useControlled.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export default function useControlled({ controlled, default: defaultProp, name,
const { current: defaultValue } = React.useRef(defaultProp);

React.useEffect(() => {
if (!isControlled && defaultValue !== defaultProp) {
// eslint-disable-next-line no-restricted-globals
const numberIsNaN = (x) => typeof x === 'number' && isNaN(x);

if (
!isControlled &&
// IE11 support. Either replace `===` with `Object.is`, or use `Number.isNaN`.
!(defaultValue === defaultProp || (numberIsNaN(defaultValue) && numberIsNaN(defaultProp)))
) {
console.error(
[
`MUI: A component is changing the default ${state} state of an uncontrolled ${name} after being initialized. ` +
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-utils/src/useControlled/useControlled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ describe('useControlled', () => {
setProps({ defaultValue: 1 });
}).not.toErrorDev();
});

it('should not raise a warning if setting NaN as the defaultValue when uncontrolled', () => {
expect(() => {
render(<TestComponent defaultValue={NaN}>{() => null}</TestComponent>);
}).not.toErrorDev();
});
});

0 comments on commit 94adbb8

Please sign in to comment.