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

@material-ui/utils: useControlled() have a different behavior than native primitives #25277

Closed
2 tasks done
layershifter opened this issue Mar 9, 2021 · 2 comments
Closed
2 tasks done
Labels
out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) package: utils Specific to the @mui/utils package

Comments

@layershifter
Copy link

Hey folks!

Thanks for your contribution into OSS, I am often gathering ideas in your code ❤️

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

I was testing different implementations of hooks that are implementing autocontrolled behavior for custom components. There is one inconsistency that I found in useControlled() between native primitives.

Current Behavior 😯

Uncontrolled component cannot become controlled 🟥

Expected Behavior 🤔

Uncontrolled component can become controlled ✅

Steps to Reproduce 🕹

I am not using an official template as CodeSandbox have been prepared before.

https://codesandbox.io/s/react-use-controlled-7sen6

Steps:

  1. Open a CodeSandbox
  2. Scroll down to "A Sandbox for input"
  3. Press "set to undefined" button (makes a component uncontrolled), press "remount" button (will change key and remount a component, it will be uncontrolled now), press "set to Foo Bar" (will make a component controlled again)
  4. Notice that an input has "Foo Baz" value ✅
  5. Try to perform the same steps for "A Sandbox for CustomInputMaterial", notice that an input's value is empty 🔴

Context 🔦

It's not affecting me in any way, but I think that it will be nice to align with the behavior of native primitives.

https://github.com/mui-org/material-ui/blob/b96916d373096570b96db101e0ceca25f7ca9346/packages/material-ui-utils/src/useControlled.js#L6

This line is responsive for the current behavior: computed once value will never change. If you have reasons to keep current behavior, please post them 😺

Your Environment 🌎

@material-ui/utils@5.0.0-alpha.27

@layershifter layershifter added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 9, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 9, 2021

@layershifter This was done on purpose, to simplify the problem.

@oliviertassinari oliviertassinari added out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) package: utils Specific to the @mui/utils package and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Mar 9, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 9, 2021

To expand a bit more on the problem. I have seen this topic pop up a number of times in the past, under different forms. Two (not directly related) I can easily find:

There are many more in the repository under the "support" or "question" label.

Historically, we have implemented it, ignoring the behavior of a native input, to simplify the problem. Controlled is defined at mount time, end of the story. This was done before we abstracted the logic under a useControlled hook, early on.

Now, It seems technically possible to match the behavior of a native input since we have the useControlled abstraction. However, I don't see any value in doing such. A single source of truth is easier to manage (for developers using the API), so I think that we might as well put constraints in place to encourage it, a pit of success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) package: utils Specific to the @mui/utils package
Projects
None yet
Development

No branches or pull requests

2 participants