Incorrect View Functon Return Type in elm

好久不见. 提交于 2020-01-06 05:36:05

问题


I am making a program that changes the rendered text on the screen to be whatever the user inputs in a text box. I think I have the model and the update part of the elm architecture correct, but I really don't understand the view portion.

I'm just having trouble wrapping my head around the square bracket view functions.

Anyway, I am getting this error.

This div call produces:

Html #(Model -> Model)#

But the type annotation on view says it should be:

Html #Msg#Elm

But I am not sure how to change my view function to return Html Msg and I am kinda confused between the difference between that and a string.

Thank you everyone!

Here is my code ...

module Main exposing (..)

import Browser
import Html exposing (Html, div, text, input, Attribute)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)

main =
  Browser.sandbox { init = init, update = update, view = view }

type alias Model = String

init : Model
init = "Hello, World!"

type alias Msg = String

update : Msg -> Model -> Model
update msg model =
  msg

view : Model -> Html Msg
view model =
  div []
    [ input [ placeholder "Input new string", value model, onInput update ] []
    , div [] [ text model ]
    ]

回答1:


You're passing the update function as an argument to onInput. Your probably meant to pass it a Msg, which the runtime will then pass to the update function.

Since your Msg type is an alias for String, you can use onInput identity



来源:https://stackoverflow.com/questions/59471927/incorrect-view-functon-return-type-in-elm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!