From f89d5eb72864804b33e1a1fb0d4d5da37c1a6cb8 Mon Sep 17 00:00:00 2001 From: Minjee Son <104430030+iammminzzy@users.noreply.github.com> Date: Sat, 8 Jun 2024 10:21:41 +0100 Subject: [PATCH] [utils] Allow passing `NaN` as `defaultValue` to `useControlled` (#41559) Co-authored-by: ZeeshanTamboli --- packages/mui-utils/src/useControlled/useControlled.js | 2 +- packages/mui-utils/src/useControlled/useControlled.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/mui-utils/src/useControlled/useControlled.js b/packages/mui-utils/src/useControlled/useControlled.js index dae7fdd1318325..48129fc05ae99c 100644 --- a/packages/mui-utils/src/useControlled/useControlled.js +++ b/packages/mui-utils/src/useControlled/useControlled.js @@ -29,7 +29,7 @@ export default function useControlled({ controlled, default: defaultProp, name, const { current: defaultValue } = React.useRef(defaultProp); React.useEffect(() => { - if (!isControlled && defaultValue !== defaultProp) { + if (!isControlled && !Object.is(defaultValue, defaultProp)) { console.error( [ `MUI: A component is changing the default ${state} state of an uncontrolled ${name} after being initialized. ` + diff --git a/packages/mui-utils/src/useControlled/useControlled.test.js b/packages/mui-utils/src/useControlled/useControlled.test.js index 7319349f7343bb..953c11f7002876 100644 --- a/packages/mui-utils/src/useControlled/useControlled.test.js +++ b/packages/mui-utils/src/useControlled/useControlled.test.js @@ -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({() => null}); + }).not.toErrorDev(); + }); });