Purescript卤素成分,输入与状态

时间:2017-11-15 17:55:47

标签: purescript halogen

我想制作一个卤素组件,其组件的输入与其状态不同。根据卤素指南(https://github.com/slamdata/purescript-halogen/blob/master/docs/5%20-%20Parent%20and%20child%20components.md#input-values),这应该是可能的。我从指南中更改了示例,如下所示

import Prelude
import Data.Int (decimal, toStringAs)
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HHE

type Input = Int

type State = String

data Query a = HandleInput Input a

component :: forall m. H.Component HH.HTML Query Input Void m
component =
  H.component
    { initialState: id
    , render
    , eval
    , receiver: HHE.input HandleInput
    }
  where

  render :: State -> H.ComponentHTML Query
  render state =
    HH.div_
      [ HH.text "My input value is:"
      , HH.strong_ [ HH.text (show state) ]
      ]

  eval :: Query ~> H.ComponentDSL State Query Void m
  eval = case _ of
    HandleInput n next -> do
      oldN <- H.get
      when (oldN /= (toStringAs decimal n)) $ H.put $ toStringAs decimal n
      pure next

但是我在, receiver: HHE.input HandleInput

的行上遇到编译错误
Could not match type

  String

with type

  Int

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

使用输入值计算initialState,并在您粘贴的代码中将其id实现,因此它尝试强制输入和状态类型为匹配。

答案 1 :(得分:0)

{ initialState: id中更改了行{ initialState: const initialState,并在where以下行后添加了

initialState :: State
initialState = ""