Closed davidbruce closed 6 months ago
Hum, I think this "bug" has been around since a long time because I saw this behaviour for a long time.
However, I am not sure if this is a bug or not. The things, is that Completed
is a class instance and when doing the HMR replacement the class reference is probably replaced.
Meaning that when the view is re-run the comparison is done between the old class instance stored in state.Filter
and the new class available coming from Completed
.
A way to check if what I am saying is correct is to move Completed
definition to another file. And trigger an HMR call from the file having the code state.Filter = Completed
. This should only hot replace the "view" file and not the file with the definition of the Completed
type.
As hinted in my previous message this is not really bug but an unfortunate effect of how Fable/F# equality works.
It is possible to remove this limitation for DUs with no arguments by transforming them into StringEnum
like that Fable will generate a string
representation of the DU case against which the equality check will works between HMR calls.
[<RequireQualifiedAccess>]
[<StringEnum>]
// Because Fable equality test the constructor reference, in watch mode we
// will get a new constructor reference every time we change the code.
// Which leads to invalid visual menu state.
// Using a string enum we can avoid this problem.
type Filter =
| All
| Completed
| NotCompleted
In F# 9, we will have access to IsXXX
members on DUs which will be implemented by checking the flag value (which is an integer
) so it will be possible to use this members to work around the limitation. But it will probably means using if
statement instead of pattern matching.
I am closing as I don't think we can do something inside of Elmish.HMR
to improve the situation unfortunately.
Description
Bool expressions are returning false unexpectedly.
For example the following will always return false when reloaded with HMR:
As a workaround I'm having to use match instead:
I noticed this issue when trying out HMR on my Todo App from the Elmish Book and the filter tabs were not getting the "is-active" class.
Repro code
Expected and actual results
Using if statements with HMR the is-active class is not reapplied:
Using match with HMR the is-active class is reapplied:
Related information