Skip to content

Commit

Permalink
[utils] Allow passing NaN as defaultValue to useControlled (@ia…
Browse files Browse the repository at this point in the history
…mmminzzy) (#42571)

Co-authored-by: Minjee Son <[email protected]>
  • Loading branch information
github-actions[bot] and iammminzzy authored Jun 8, 2024
1 parent 177d8e9 commit be564e3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/mui-utils/src/useControlled/useControlled.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. ` +
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 be564e3

Please sign in to comment.