-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Unexpected conversion of Date
to string in optimistic updates in useUpdate
and Forms
#10478
Comments
Thanks for your question. We use react-query for API calls. API responses use JSON, and therefore when react-admin receives a date from the backend, it is always serialized as a string. And when react-admin sends a date, it is also serialized. The optimistic update follows this principle. I don't see any way around this architecture limitation. |
Hey @fzaninotto, thank you for your quick response. This also means that every form needs to output If I know that this is the intended way, I can work with that. But I think this is a problem others might also be running into, as it is not quite obvious to know, that my form can never output I do feel like replacing the line: const clonedData = JSON.parse(JSON.stringify(data)); with something that simply removes In any way I think it might be beneficial to make this assumption more clear in the data provider documentation, as, as far as I know, quite a few OpenAPI or RPC Generators like to transform Date fields to javascript What do you think? I am also happy to open a pull request. |
No, react-admin is backend agnostic. However we do have some defaults on the
This is the default behavior of HTML date inputs, yes.
It depends on your dataProvider
We do, by default. Those inputs can leverage
This is a good point: the conversion to JSON will indeed incur a conversion of dates to string. In that sense, we do impose it for no reason. @fzaninotto or @slax57 what do you think? |
The purpose of this line
Is to remove undefined values. If you can find a better way to do it without serializing dates, it's not a breaking change, and I agree that it makes That being said, the check for undefined values must be done for all values, even nested ones. We'll welcome a PR for this change. |
What you were expecting:
Hey there, first off, thank you very much for all the great work you are doing on react-admin. I very much appreciate it and love what you have built!
I am experiencing problems due to how react-admin handles optimistic updates and just wanted to raise awarness for this.
The problem lies in the code below here:
react-admin/packages/ra-core/src/dataProvider/useUpdate.ts
Line 127 in 107db17
I have objects in the form of:
Now, I have a form which i use to update these appointments which also outputs
start
andend
as javascriptDate
types.I was expecting, that optimisic updates based on this would keep the types intact and save an updated object of the same shape into the query cache.
What happened instead:
When react-admin performs its optimisic update and updates the
@tanstack/react-query
cache, it does it using the code snippet above to strip allundefined
values. Unfortunately in the process, through theJSON.stringify
, it also transforms myDate
s intostring
s.So now I have an appointment in the form of:
in my cache until the response from the server comes along and places a new object with
Date
s in it again. Unfortunately in the meantime the date from the optimistic updates, leads to unexpected behaviour if it isn't handled correctly.My question now is: If the only purpose of the
JSON.parse(JSON.stringify(...))
is, to removeundefined
values. Wouldn't it make sense to recursively removeundefined
values and keep the other types intact? I like havingDate
s everywhere in my application and do the conversion to string right in the API Layer but this behaviour currently messes with that workflow.What are your thoughts on this? Is this the desired behaviour or could we improve on it?
Thank your very much!
Environment
The text was updated successfully, but these errors were encountered: