anderson
anderson

Reputation: 465

How do you render to root page in Elm?

I am super Elm beginner and struggling with a following thing.

I want to render to root page after I get a HTTP response from server. (Not right after DOM event happens)

I am using Browser.application and trying to goTo function but I don’t know how it can be done.

-- I want to render to root page after I get a HTTP response. (Maybe this Cmd.none can be replaced by some function that renders to root page?)
GotResponse result ->
    case result of
        Ok response ->
            ( { model | response = response }, Cmd.none )

        Err _ ->
            ( model, Cmd.none )

Does anybody have any ideas?

Upvotes: 0

Views: 123

Answers (1)

Sidney
Sidney

Reputation: 4775

You're correct! You'll need to do that with a command.

You probably want to use Browser.Navigation.pushUrl. Here's how it would look in your code:

GotResponse result ->
    case result of
        Ok response ->
            ( { model | response = response }
            , Browser.Navigation.pushUrl model.key "/"
            )

        Err _ ->
            ( model, Cmd.none )

You should have the Key in your model, which is required by the Elm runtime to make sure you're using Browser.application (which is explained here)

Upvotes: 1

Related Questions