Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[utils] Allow passing NaN as defaultValue to useControlled #41559

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

iammminzzy
Copy link
Contributor

@iammminzzy iammminzzy commented Mar 19, 2024

This fixes an issue with a warning getting logged when passing NaN as defaultValue to useControlled due to using === instead of Object.is. However, Object.is is not supported in IE11 so I made the change in a backwards-compatible way.

@mui-bot
Copy link

mui-bot commented Mar 19, 2024

Netlify deploy preview

https://deploy-preview-41559--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against ea98fb5

@iammminzzy iammminzzy changed the title [utils] Allow passing NaN as defaultValue to useControlled [utils] Allow passing NaN as defaultValue to useControlled Mar 19, 2024
@zannager zannager added the package: material-ui Specific to @mui/material label Mar 19, 2024
@NMinhNguyen
Copy link
Contributor

NMinhNguyen commented Mar 19, 2024

Just curious - are all changes supposed to go into next branch starting from today, and not master anymore? If next, then I guess IE support could be dropped already.. And if not, then should this PR target master?

Comment on lines +109 to +113
it('should not raise a warning if setting NaN as the defaultValue when uncontrolled', () => {
expect(() => {
render(<TestComponent defaultValue={NaN}>{() => null}</TestComponent>);
}).not.toErrorDev();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should it support this? It looks like more bundle size and complexity for no clear value in exchange.

Object.is is not supported in IE11

We don't support IE 11 anymore, so maybe Object.is would make it, a light DX improvement for no real UX cost.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example is if you have a Select component where one of the options has value of NaN and you set the defaultValue to NaN you would incorrectly get the "component is changing the default value state" warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundle size shouldn’t be impacted at all because the whole effect is wrapped in a process.env.NODE_ENV check, and nowhere else in the codebase uses Object.is and there are IE11 comments, so we figured it’s better to keep the partial IE11 support but more than happy to use Object.is (that’s what React uses for dependency arrays, for example)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example is if you have a Select component where one of the options has value of NaN

Isn't it wrong that an option has a value of NaN?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example is if you have a Select component where one of the options has value of NaN

Isn't it wrong that an option has a value of NaN?

Respectfully, I don't think there's anything wrong with using NaN as a value. For example I could use it to represent a "None" value, or maybe the number value comes from user input and NaN represents an invalid input.

@oliviertassinari oliviertassinari added package: base-ui Specific to @mui/base and removed package: material-ui Specific to @mui/material labels Mar 19, 2024
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide a reproduction? This StackBlitz sandbox template may be a good starting point.

@QingqiShi
Copy link

QingqiShi commented Apr 28, 2024

Could you provide a reproduction? This StackBlitz sandbox template may be a good starting point.

Simple repro with the Autocomplete component, but essentially anything that uses the useControlled hook suffers from the same issue. https://stackblitz.com/edit/stackblitz-starters-unpbpo?file=src%2FApp.tsx

@ZeeshanTamboli ZeeshanTamboli added package: utils (private) Specific to the private @mui/utils package enhancement This is not a bug, nor a new feature and removed package: base-ui Specific to @mui/base labels Apr 30, 2024
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me, but since it's also used in MUI X, it should be reviewed by others too. cc @flaviendelangle @Janpot (Tagging Jan because it might be used in Toolpad too).

Copy link
Member

@Janpot Janpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no problems with this, the warning is about comparing values of props between renders and React also use Object.is for this purpose. It doesn't affect production bundle sizes.
Doesn't mean I think it's a good idea to use NaN as a value to represent state, but it should probably be left to the consumer of this hook to decide how to handle it.

@mj12albert
Copy link
Member

Doesn't mean I think it's a good idea to use NaN as a value to represent state, but it should probably be left to the consumer of this hook to decide how to handle it.

I agree 👍 generally we are moving towards using null for "no value" (e.g. Material UI's Select vs Base UI's Select) but it's totally the user's decision ~
(I think I've seen RA use NaN internally for "no value" as well)

@NMinhNguyen
Copy link
Contributor

Completely contrived example but you could imagine an educational calculator component that lets you pick from values like NaN, -10, 10 to see the impact of multiplying it by another hard-coded number. Sure, you could write code to treat null as NaN, but seems a bit annoying 😛

@ZeeshanTamboli
Copy link
Member

@mj12albert Since you approved it, could you merge it? Let's not leave it hanging, unless you're waiting for something specific. Should we also consider cherry-picking this to the master branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature package: utils (private) Specific to the private @mui/utils package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants