0

我想制作一个卤素组件,其中组件的输入与其状态不同。根据卤素指南(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

我在这里做错了什么?

4

2 回答 2

1

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

于 2017-11-17T01:28:49.677 回答
0

更改行并在{ initialState: id以下行之后添加{ initialState: const initialStatewhere

initialState :: State
initialState = ""
于 2017-11-17T17:05:33.600 回答