mobx
mobx copied to clipboard
Use getTime() to check for Date equality
Intended outcome:
When updating observables from an API response which contains timestamps,
if a timestamp in the response is unchanged since the last time I updated,
I don't want autorun
to trigger.
Actual outcome:
Because I cast the timestamps to a JS Date
,
autorun
is triggered,
even if the getTime()
is the same.
How to reproduce the issue:
Open console on: https://codesandbox.io/s/autorun-new-date-2np493?file=/src/index.js
Observe:
-
autorun
doesn't log when clicking Finshed/Unfinished if already finished/unfinished - 'Set now' buttons will always
autorun
because time is changing - 'Set same' buttons differ in behavior: if 'Date' then will always
autorun
, if 'Time' then won't.
I'd argue that for 99.9999% of use cases you wouldn't want autorun
to trigger,
because the actual 'value' hasn't changed.
See also:
- https://github.com/mobxjs/mobx-state-tree/issues/2080