I know how to avoid/fix that and how/when to use controlled or uncontrolled input, but changing between uncontrolled and controlled input clearly works, so why is it not officially supported?
And not only that, but some React component libraries, such as Atlaskit provide inputs that exhibit this behavior.
Not even the official docs mention Why it matters react.dev~changing-an-uncontrolled-input-to-be-controlled
If you provide a
value
to the component, it must remain a string throughout its lifetime.You cannot pass
value={undefined}
first and later passvalue="some string"
because React won’t know whether you want the component to be uncontrolled or controlled. A controlled component should always receive a stringvalue
, notnull
orundefined
.If your
value
is coming from an API or a state variable, it might be initialized tonull
orundefined
. In that case, either set it to an empty string (''
) initially, or passvalue={someValue ?? ''}
to ensure value is a string.Similarly, if you pass
checked
to a checkbox, ensure it’s always a boolean.
2
Answers
In React, switching between controlled and uncontrolled input might result in unexpected behavior and make it more difficult to maintain your code for the following reasons:
State Management: Controlled components in React rely on managing the state of the input value within the component’s state or props. Uncontrolled components, on the other hand, manage their own state internally. Switching between the two can result in confusion and inconsistency in how you handle and update the input value’s state.
Data Synchronization: When using controlled components, you have explicit control over the input value and can easily synchronize it with other parts of your application state. Switching to an uncontrolled component would require additional effort to keep the input value in sync with your application’s data flow.
Validation and Error Handling: Controlled components make it easier to implement validation and error handling. You can validate the input value before updating the state and provide immediate feedback to the user. With uncontrolled components, you may need to rely on other mechanisms to handle validation and error scenarios.
Testing and Debugging: Controlled components offer better testability and debugging capabilities. Since the input value is controlled by the component’s state, you can easily simulate different scenarios and test the behavior. Uncontrolled components can be more challenging to test and debug, as their state is managed internally.
Code Consistency and Readability: Consistency in your codebase is crucial for maintainability. Switching back and forth between uncontrolled and controlled inputs can make the codebase harder to read and understand, especially for other developers who may work on the same codebase.
I have tested that switching from a controlled component to an uncontrolled component will not throw an warning, but on the contrary it will. I guess it is because the user may forget to handle the onchange event from uncontrolled to controlled, resulting in a bug,so React throw a warn to help developer avoid these issue.