Pattern: ISO-string date fields to avoid Date-object reactivity issues #2151
Replies: 1 comment
|
To your second question directly: no, there's no framework-level reason TanStack Form would prefer Date objects, the library is generic over field value types, it doesn't special-case So your ISO-string pattern isn't working around a documented preference, it's sidestepping a general JS footgun (reference vs. value equality on Date objects) that would show up the same way in any comparison that isn't Date-aware, TanStack Form included. Given that, it sounds like a genuinely useful addition to |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Has anyone else run into Date-object reactivity issues with TanStack Form's date fields? Closed issue #1712 ("isDefaultValue not updating when changing date field") surfaces a class of problem that comes from comparing Date instances by reference rather than value.
I work on Kalyx (a headless React DatePicker) and we took a different approach there — wanted to share the pattern in case it fits as a recipe or documentation note here.
The pattern
Use ISO 8601 UTC strings as the field value type, never Date objects:
Two things it gets you:
defaultValue === currentValueis plain string comparison, soisDefaultValue/isPristinetrack correctly. The class of bug inisDefaultValuenot updating when changing date field #1712 doesn't apply.z.iso.datetime()validates without a Date round-trip, and you can serialize to JSON without manual.toISOString()calls.Asking
examples/react/? There's asimpleexample but no date-field one specifically, and date-related issues seem to converge on this same shape.Happy to put together a self-contained
examples/react/date-fields-iso/example. Kalyx works as the picker because of the ISO contract, but the pattern is library-agnostic — anything that emits strings would do.All reactions