slaveOftime / Fun.Blazor

Powered by .NET blazor!!! ❤ F#
https://slaveoftime.github.io/Fun.Blazor.Docs/
MIT License
189 stars 7 forks source link

First elmish state update discarded #25

Closed omcnoe closed 2 years ago

omcnoe commented 2 years ago

The first state update is discarded, unless you use the html.elmish constructor that includes an initial Cmd<Msg>, AND the Msg is not a noop.

Fun.Blazor.Wasm 2.0.0-beta008

Simple repro:

type State =
    { Count: int }
    static member init = { Count = 0 }

type Msg = SetCount of int

let update msg state =
    match msg with
    | SetCount count -> { state with Count = count }, Cmd.none

let render state (dispatch: Msg -> unit) =
    Template.html
        $"""
        <div>
            {state.Count} <button onclick={fun _ -> SetCount(state.Count + 1) |> dispatch}>+</button>
        </div>"""

let app =
    html.elmish ((fun _ -> State.init, Cmd.none), update, render)
albertwoo commented 2 years ago

But please keep in mind this project is very young and in beta, so there are some bugs or even breaking changes. Thanks for you feedback. :)

omcnoe commented 2 years ago

I have all the necesary packages in the .fsproj

<PackageReference Include="Fun.Blazor.Elmish" Version="2.0.0-beta*" />
 <PackageReference Include="Fun.Blazor.HtmlTemplate" Version="2.0.0-beta008" />
 <PackageReference Include="Fun.Blazor.Wasm" Version="2.0.0-beta*" />

And my code does open the necessary modules. Sorry just got cropped out of the example.

I think it is an actual bug if init Cmd is Cmd.none :) Understood that project is in beta.

albertwoo commented 2 years ago

Sorry I misunderstanded your point. I can reproduce that now. It is here: https://github.com/slaveOftime/Fun.Blazor/blob/29068b94fae0974323399ae116c4b729c24e4244/Fun.Blazor.Elmish/ElmComponent.fs#L31-L44 I will check how elmish work again and try to fix it. If you are very familiar with elmish your help will be very welcomed :)

albertwoo commented 2 years ago

@omcnoe hi, I changed the init a little bit. The bug should be fixed.

But I will lerarn more on elmish to improve it when I got more time.

omcnoe commented 2 years ago

Yes, issue is fixed. Thanks.