-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat(react): add useControllableState #7980
feat(react): add useControllableState #7980
Conversation
…feat/add-use-controllable-state
Deploy preview for carbon-elements ready! Built with commit 78984d6 |
Deploy preview for carbon-components-react ready! Built with commit 78984d6 https://deploy-preview-7980--carbon-components-react.netlify.app |
Deploy preview for carbon-elements ready! Built with commit 522af92 |
Deploy preview for carbon-components-react ready! Built with commit 522af92 https://deploy-preview-7980--carbon-components-react.netlify.app |
Thanks for walking me through this! |
We have several components that can be controlled or uncontrolled and have built up a couple of patterns for implementing this throughout the codebase. This PR introduces a custom
useControllableState
hook to start the discussion around what it would look like to standardize this pattern across the codebase.For controlled versus uncontrolled components, a basic example of this is an
<input>
. This primitive can be uncontrolled or controlled:One important note with this: switching from controlled to uncontrolled, or vice versa, is not supported.
To replicate this behavior for components we build, we would use
useControllableState
:We can then use the same component,
TextInput
, for both controlled and uncontrolled scenarios:Note: this was heavily inspired by a similar hook in [Chakra UI](https://chakra-ui.com/docs/hooks/use-controllable
Changelog
New
useControllableState
hook for components that can be controlled or uncontrolledChanged