Skip to content

Commit be564e3

Browse files
[utils] Allow passing NaN as defaultValue to useControlled (@iammminzzy) (#42571)
Co-authored-by: Minjee Son <[email protected]>
1 parent 177d8e9 commit be564e3

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

packages/mui-utils/src/useControlled/useControlled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function useControlled({ controlled, default: defaultProp, name,
2929
const { current: defaultValue } = React.useRef(defaultProp);
3030

3131
React.useEffect(() => {
32-
if (!isControlled && defaultValue !== defaultProp) {
32+
if (!isControlled && !Object.is(defaultValue, defaultProp)) {
3333
console.error(
3434
[
3535
`MUI: A component is changing the default ${state} state of an uncontrolled ${name} after being initialized. ` +

packages/mui-utils/src/useControlled/useControlled.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ describe('useControlled', () => {
105105
setProps({ defaultValue: 1 });
106106
}).not.toErrorDev();
107107
});
108+
109+
it('should not raise a warning if setting NaN as the defaultValue when uncontrolled', () => {
110+
expect(() => {
111+
render(<TestComponent defaultValue={NaN}>{() => null}</TestComponent>);
112+
}).not.toErrorDev();
113+
});
108114
});

0 commit comments

Comments
 (0)