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:
value
prop oninput
should not be null. Consider using an empty string to clear the component orundefined
for 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
defaultValue
of the input, ordefaultValues
of 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
null
values 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)
While I understand this reasoning, this leads to major headaches for some users though. Our API returns a lot of
null
values 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
null
value like an empty string whennull
is 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
useInput
level that makes it easy to add this? Something liketreatNullAsEmpty
? And set this in all components wherenull
is no sensible option, likeTextInput
andBooleanInput
.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: