Warning when using null value on TextInput
See original GitHub issueWhat you were expecting:
When my API returns null as the value for a <TextInput />, it should not lead to any warnings.
What happened instead:
When a record is returned by the API with a value of null, this leads to a warning when used on a text input:
Warning:
valueprop oninputshould not be null. Consider using an empty string to clear the component orundefinedfor uncontrolled components.
and
Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.
Steps to reproduce:
- Go to the sandbox provided
- Open any post to edit
- Observe console output (you may need to filter for
should not be null)
Related code:
https://codesandbox.io/s/hopeful-worker-u1dxlq?file=/src/posts/PostEdit.tsx
Other information:
- Setting the
defaultValueof the input, ordefaultValuesof the form to an empty string (''), doesn’t make the warning go away - From the migration docs it’s clear that the behaviour changed in regard of how
nullvalues are treated by react-form-hook compared to react-final-form. But it’s unclear to me how to mitigate this warning.
Environment
- React-admin version: 4.1.2
- Last version that did not exhibit the issue (if applicable): 3.19.x
- React version: 17
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:11 (7 by maintainers)

Top Related StackOverflow Question
While I understand this reasoning, this leads to major headaches for some users though. Our API returns a lot of
nullvalues for fields where no content exists. We would have to turn all of those into empty strings manually in the dataProvider, just so that they can be used in TextInputs. Sure, it’s possible, but seems like a lot of work for something that react-admin could easily help us with.Is there any reason not to treat a
nullvalue like an empty string whennullis not a valid option? (like in TextInputs) This seems like a major convenience feature that not only helps people that upgraded from v3, but also everyone else that has null values in their API.Maybe we could have a prop on the
useInputlevel that makes it easy to add this? Something liketreatNullAsEmpty? And set this in all components wherenullis no sensible option, likeTextInputandBooleanInput.If a change in the default behavior of those components is acceptable, I would consider working on a PR for this. WDYT?
In the meantime we are using custom wrapper components like this to work around the problem: